Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ on:
env:
APP_NAME: module-core-loraraw
GHCR_IMAGE: ghcr.io/nubeio/module-core-loraraw
PLATFORMS: linux/amd64,linux/arm/v7
PLATFORMS: linux/amd64

jobs:

context:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

outputs:
shouldBuild: ${{ steps.context.outputs.decision_build }}
Expand All @@ -36,7 +36,7 @@ jobs:
fqn: ${{ env.APP_NAME }}-${{ steps.context.outputs.version }}-${{ steps.context.outputs.shortCommitId }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
token: ${{ secrets.NUBEIO_CI_GITHUBPROJECT_TOKEN }}

Expand All @@ -58,7 +58,7 @@ jobs:
defaultBranch: master

build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
needs: context
if: needs.context.outputs.shouldBuild == 'true'
env:
Expand All @@ -72,37 +72,22 @@ jobs:
-v /home/runner:/var/lib/registry
--name registry
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.16.6'
- uses: actions/checkout@v4

- name: Set current date as env variable
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%dT%H:%M:%S')"

- name: Edit main.go for environment
env:
VERSION: ${{ needs.context.outputs.version }}
COMMIT: ${{ needs.context.outputs.shortCommitId }}
BUILD_DATE: ${{ steps.date.outputs.date }}
run: |
sed -i -e 's,<version>,'"${VERSION}"',' main.go
sed -i -e 's/<commit>/'"${COMMIT}"'/' main.go
sed -i -e 's/<build_date>/'"${BUILD_DATE}"'/' main.go

- name: Build amd64
- name: Build inside Docker
run: |
git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/NubeIO".insteadOf "https://github.com/NubeIO"
go mod tidy
go build -o module-core-loraraw-amd64
docker build \
-f Dockerfile \
-t build-image .

- name: Build armv7
if: ${{ needs.context.outputs.isRelease == 'true' }}
run: |
sudo apt-get update -y
sudo apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
env GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ go build -o module-core-loraraw-armv7
container_id=$(docker create build-image)
docker cp $container_id:/app/module-core-loraraw-amd64 ./module-core-loraraw-amd64
docker cp $container_id:/app/module-core-loraraw-armv7 ./module-core-loraraw-armv7
docker rm $container_id

- name: Zip artifacts
if: ${{ needs.context.outputs.isRelease == 'true' }}
Expand Down
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Use Ubuntu 20.04 as the base image
FROM ubuntu:20.04 AS builder

# Install Go and necessary dependencies for cross-compilation
RUN apt-get update
RUN apt-get install -y \
gcc \
gcc-x86-64-linux-gnu \
gcc-arm-linux-gnueabihf \
g++-arm-linux-gnueabihf \
wget

# Set the working directory
WORKDIR /app

# Copy the Go project files
COPY . .

# Define build arguments
ARG VERSION
ARG COMMIT
ARG BUILD_DATE

RUN wget https://go.dev/dl/go1.21.13.linux-amd64.tar.gz
RUN tar -C /usr/local -xzf go1.21.13.linux-amd64.tar.gz && \
rm go1.21.13.linux-amd64.tar.gz

# Set Go environment variables
ENV PATH=$PATH:/usr/local/go/bin
ENV GOPATH=/go

# Run Go mod tidy and build for the default platform (amd64)
RUN go mod tidy
RUN env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 \
CC=x86_64-linux-gnu-gcc CXX=x86_64-linux-gnu-g++ \
go build -o module-core-loraraw-amd64

# Build for ARMv7 architecture
RUN env GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 \
CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
go build -o module-core-loraraw-armv7
23 changes: 22 additions & 1 deletion aesutils/aesutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func prepareCMAC(data, key []byte) ([]byte, error) {
return nil, err
}

// Create CMAC object
// Create a CMAC object
cmacObj, err := cmac.New(block)
if err != nil {
return nil, err
Expand All @@ -124,3 +124,24 @@ func prepareCMAC(data, key []byte) ([]byte, error) {
mac := cmacObj.Sum(nil)
return mac[:4], nil
}

func CmacUnencrypted(data []byte, key []byte) ([]byte, error) {
// Create AES cipher
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

// Create a CMAC object
mac, err := cmac.New(block)
if err != nil {
return nil, err
}

// Update the CMAC object with the data
mac.Write(data)

// Compute the MAC and return the first 4 bytes
fullMAC := mac.Sum(nil)
return fullMAC[:4], nil
}
6 changes: 4 additions & 2 deletions endec/base.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package endec

import (
"github.com/NubeIO/nubeio-rubix-lib-models-go/model"
"strconv"

"github.com/NubeIO/nubeio-rubix-lib-models-go/model"
)

const (
Expand Down Expand Up @@ -32,8 +33,9 @@ func DecodePayload(
mtFn UpdateDeviceMetaTagsFunc,
dequeueFn DequeuePointWriteFunc,
internalPointUpdateFn InternalPointUpdate,
sendAckMessageFn SendAckToDeviceFunc,
) error {
err := devDesc.Decode(data, devDesc, device, fn, mtFn, dequeueFn, internalPointUpdateFn)
err := devDesc.Decode(data, devDesc, device, fn, mtFn, dequeueFn, internalPointUpdateFn, sendAckMessageFn)
return err
}

Expand Down
4 changes: 4 additions & 0 deletions endec/devices_and_points.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package endec

import (
"errors"

"github.com/NubeIO/module-core-loraraw/schema"
"github.com/NubeIO/nubeio-rubix-lib-models-go/model"

Expand All @@ -12,6 +13,7 @@ type UpdateDevicePointFunc func(name string, value float64, device *model.Device
type UpdateDeviceMetaTagsFunc func(uuid string, metaTags []*model.DeviceMetaTag) error
type DequeuePointWriteFunc func(messageId uint8) *model.Point
type InternalPointUpdate func(point *model.Point) (*model.Point, error)
type SendAckToDeviceFunc func(device *model.Device, messageId uint8) error

type LoRaDeviceDescription struct {
DeviceName string
Expand All @@ -26,6 +28,7 @@ type LoRaDeviceDescription struct {
updateDevMetaTagsFnc UpdateDeviceMetaTagsFunc,
dequeuePointWriteFunc DequeuePointWriteFunc,
internalPointWriteFunc InternalPointUpdate,
sendAck SendAckToDeviceFunc,
) error
GetPointNames func() []string
IsLoRaRAW bool
Expand All @@ -52,6 +55,7 @@ func NilLoRaDeviceDescriptionDecode(
_ UpdateDeviceMetaTagsFunc,
_ DequeuePointWriteFunc,
_ InternalPointUpdate,
_ SendAckToDeviceFunc,
) error {
return errors.New("nil decode function called")
}
Expand Down
8 changes: 7 additions & 1 deletion endec/droplet.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package endec

import (
"strconv"

"github.com/NubeIO/module-core-loraraw/utils"
"github.com/NubeIO/nubeio-rubix-lib-models-go/model"
"strconv"
)

const (
Expand Down Expand Up @@ -49,6 +50,7 @@ func DecodeDropletTH(
_ UpdateDeviceMetaTagsFunc,
_ DequeuePointWriteFunc,
_ InternalPointUpdate,
_ SendAckToDeviceFunc,
) error {
temperature, err := dropletTemp(data)
if err != nil {
Expand Down Expand Up @@ -83,6 +85,7 @@ func DecodeDropletTHL(
updateDeviceMetaTagFn UpdateDeviceMetaTagsFunc,
dequeuePointWriteFunc DequeuePointWriteFunc,
internalPointUpdate InternalPointUpdate,
sendAck SendAckToDeviceFunc,
) error {
err := DecodeDropletTH(
data,
Expand All @@ -92,6 +95,7 @@ func DecodeDropletTHL(
updateDeviceMetaTagFn,
dequeuePointWriteFunc,
internalPointUpdate,
sendAck,
)
if err != nil {
return err
Expand All @@ -112,6 +116,7 @@ func DecodeDropletTHLM(
updateDeviceMetaTagsFn UpdateDeviceMetaTagsFunc,
dequeuePointWriteFunc DequeuePointWriteFunc,
internalPointUpdate InternalPointUpdate,
sendAck SendAckToDeviceFunc,
) error {
err := DecodeDropletTHL(
data,
Expand All @@ -121,6 +126,7 @@ func DecodeDropletTHLM(
updateDeviceMetaTagsFn,
dequeuePointWriteFunc,
internalPointUpdate,
sendAck,
)
if err != nil {
return err
Expand Down
33 changes: 17 additions & 16 deletions endec/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package endec

import (
"errors"
"math"
"strconv"
"unsafe"

"github.com/NubeIO/lib-utils-go/nstring"
"github.com/NubeIO/module-core-loraraw/aesutils"
"github.com/NubeIO/module-core-loraraw/utils"
"github.com/NubeIO/nubeio-rubix-lib-models-go/model"
"math"
"strconv"
"unsafe"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -111,7 +112,7 @@ func dataTypeToBits[T any](data T, metaData *MetaData, data64 *uint64, bitCount
return true
}

func encodeData[T any](serialData *SerialData, data T, header MetaDataKey, position uint8) bool {
func EncodeData[T any](serialData *SerialData, data T, header MetaDataKey, position uint8) bool {
metaData := getMetaData(header)
headerVector := make([]byte, 0)
dataVector := make([]byte, 0)
Expand All @@ -130,17 +131,17 @@ func encodeData[T any](serialData *SerialData, data T, header MetaDataKey, posit
switch v := any(data).(type) {
case float64:
if !fixedPointToBits(v, &metaData, &dataBits, &bitCount) {
log.Errorf("encodeData: fixedPointToBits failed for float64")
log.Errorf("EncodeData: fixedPointToBits failed for float64")
return false
}
case float32:
if !fixedPointToBits(v, &metaData, &dataBits, &bitCount) {
log.Errorf("encodeData: fixedPointToBits failed for float32")
log.Errorf("EncodeData: fixedPointToBits failed for float32")
return false
}
default:
log.Errorf("%v", v)
log.Errorf("encodeData: Unsupported type for FIXEDPOINT: %T", data)
log.Errorf("EncodeData: Unsupported type for FIXEDPOINT: %T", data)
return false
}
// Add header to buffer
Expand Down Expand Up @@ -186,23 +187,23 @@ func EncodeAndEncrypt(point *model.Point, serialData *SerialData, key []byte) ([
}

if MetaDataKey(pointDataType) == MDK_UINT_8 {
encodeData(serialData, uint8(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, uint8(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_UINT_16 {
encodeData(serialData, uint16(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, uint16(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_UINT_32 {
encodeData(serialData, uint32(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, uint32(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_UINT_64 {
encodeData(serialData, uint64(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, uint64(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_INT_8 {
encodeData(serialData, int8(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, int8(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_INT_16 {
encodeData(serialData, int16(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, int16(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_INT_32 {
encodeData(serialData, int32(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, int32(*writeValue), MetaDataKey(pointDataType), addressID)
} else if MetaDataKey(pointDataType) == MDK_INT_64 {
encodeData(serialData, int64(*writeValue), MetaDataKey(pointDataType), addressID)
EncodeData(serialData, int64(*writeValue), MetaDataKey(pointDataType), addressID)
} else {
encodeData(serialData, *writeValue, MetaDataKey(pointDataType), addressID)
EncodeData(serialData, *writeValue, MetaDataKey(pointDataType), addressID)
}

encryptedData, err := aesutils.Encrypt(
Expand Down
4 changes: 3 additions & 1 deletion endec/microedge.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package endec

import (
"strconv"

"github.com/NubeIO/module-core-loraraw/schema"
"github.com/NubeIO/nubeio-rubix-lib-helpers-go/pkg/nube/thermistor"
"github.com/NubeIO/nubeio-rubix-lib-models-go/datatype"
"github.com/NubeIO/nubeio-rubix-lib-models-go/model"
"strconv"
)

const (
Expand Down Expand Up @@ -41,6 +42,7 @@ func DecodeME(
_ UpdateDeviceMetaTagsFunc,
_ DequeuePointWriteFunc,
_ InternalPointUpdate,
_ SendAckToDeviceFunc,
) error {
p, err := pulse(data)
if err != nil {
Expand Down
Loading
Loading