Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gpio mappers for huawei atlas200DKOrsline merge #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions mapper-sdk-go/internal/mqttadapter/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func SyncInfo(dic *di.Container, message mqtt.Message) {
if i == len(deviceInstances[instanceID].Twins) {
continue
}
// Desired value is not changed.
if deviceInstances[instanceID].Twins[i].Desired.Value == twinValue {
continue
}
if len(twinValue) > 30{
klog.V(4).Infof("Set %s:%s value to %s......", instanceID, twinName, twinValue[:30])
}else{
Expand Down
1 change: 1 addition & 0 deletions mappers/gige/driver/gigeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func (gigEClient *GigEVisionDevice) PostImage(DeviceSN string) {
go func() {
var buffer []byte
var bufferHdr = (*reflect.SliceHeader)(unsafe.Pointer(&buffer))
defer C.free_image((**C.char)(unsafe.Pointer(&imageBuffer)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This func can free the memory after the func ends and avoid the problem of memory leak

bufferHdr.Data = uintptr(unsafe.Pointer(imageBuffer))
bufferHdr.Len = size
bufferHdr.Cap = size
Expand Down
11 changes: 11 additions & 0 deletions mappers/gpio-atlas200/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:20.04
WORKDIR /usr/local/bin
COPY ./res /usr/local/res
COPY ./bin /usr/local/bin
#RUN echo "HwHiAiUser:x:1000:1000::/home/HwHiAiUser:" >> /etc/passwd ; \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'HwHiAiUser’ used for atlas200DK?

#echo "HwHiAiUser:x:1024:" >> /etc/group ; \
#echo "HwHiAiUser:!:17795:0:99999:7:::" >> /etc/shadow ; \
#chown -R 1000:1000 /usr/local/bin; \
#chown -R 1000:1000 /usr/local/res;
USER 0
ENTRYPOINT ["./gpio","--v","4"]
36 changes: 36 additions & 0 deletions mappers/gpio-atlas200/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
SHELL := /bin/bash

curr_dir := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
rest_args := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
$(eval $(rest_args):;@:)

help:
#
# Usage:
# make template : create a mapper based on a template.
# make mapper {mapper-name} <action> <parameter>: execute mapper building process.
# make all : execute building process to all mappers.
#
# Actions:
# - mod, m : download code dependencies.
# - lint, l : verify code via go fmt and `golangci-lint`.
# - build, b : compile code.
# - package, p : package docker image.
# - test, t : run unit tests.
# - clean, c : clean output binary.
#
# Parameters:
# ARM : true or undefined
# ARM64 : true or undefined
#
# Example:
# - make mapper gpio ARM64=true : execute `build` "gpio" mapper for ARM64.
# - make mapper gpio test : execute `test` "gpio" mapper.
@echo

make_rules := $(shell ls $(curr_dir)/hack/make-rules | sed 's/.sh//g')
$(make_rules):
@$(curr_dir)/hack/make-rules/[email protected] $(rest_args)

.DEFAULT_GOAL := help
.PHONY: $(make_rules) build test package
12 changes: 12 additions & 0 deletions mappers/gpio-atlas200/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/kubeedge/mappers-go/mapper-sdk-go/pkg/service"
"github.com/kubeedge/mappers-go/mappers/gpio-atlas200/driver"
)

// main gpio device program entry
func main() {
gpio := &driver.GPIO{}
service.Bootstrap("GPIO", gpio)
}
60 changes: 60 additions & 0 deletions mappers/gpio-atlas200/crd_example/gpio-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gpio-deploy-edge-line
spec:
replicas: 1
selector:
matchLabels:
app: gpio-line1
template:
metadata:
labels:
app: gpio-line1
spec:
hostNetwork: true
nodeSelector:
kubernetes.io/hostname: edge-a200-00
containers:
- name: gpio-container
image: gpio-mapper-arm64:v1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 1215
hostPort: 1215
hostIP: 0.0.0.0
securityContext:
privileged: true
volumeMounts:
- name: config-volume
mountPath: /opt/kubeedge/
- name: gpio0-direction
mountPath: /sys/class/gpio/gpio504/direction
- name: gpio1-direction
mountPath: /sys/class/gpio/gpio444/direction
- name: gpio0-value
mountPath: /sys/class/gpio/gpio504/value
- name: gpio1-value
mountPath: /sys/class/gpio/gpio444/value
- name: i2c-path
mountPath: /dev/i2c-1
volumes:
- name: config-volume
configMap:
name: device-profile-config-edge-a200-00
- name: gpio0-direction
hostPath:
path: /sys/class/gpio/gpio504/direction
- name: gpio1-direction
hostPath:
path: /sys/class/gpio/gpio444/direction
- name: gpio0-value
hostPath:
path: /sys/class/gpio/gpio504/value
- name: gpio1-value
hostPath:
path: /sys/class/gpio/gpio444/value
- name: i2c-path
hostPath:
path: /dev/i2c-1
restartPolicy: Always
78 changes: 78 additions & 0 deletions mappers/gpio-atlas200/crd_example/gpio-device-instance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
apiVersion: devices.kubeedge.io/v1alpha2
kind: Device
metadata:
name: led-instance-01
labels:
model: led-model
spec:
deviceModelRef:
name: led-model
protocol:
customizedProtocol:
protocolName: GPIO
nodeSelector:
nodeSelectorTerms:
- matchExpressions:
- key: ''
operator: In
values:
- edge-a200-00 #pls give your edge node name
propertyVisitors:
- propertyName: red-power-status
collectCycle: 10000000000 #Mapper 从设备收集数据的频率
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the comment in English

reportCycle: 1000000000 #Mapper 报告数据的频率
customizedProtocol:
protocolName: GPIO
configData:
pin: 0 #pls give your red light's Pin
- propertyName: green-power-status
collectCycle: 10000000000 #Mapper 从设备收集数据的频率
reportCycle: 1000000000 #Mapper 报告数据的频率
customizedProtocol:
protocolName: GPIO
configData:
pin: 1 #pls give your green light's Pin
- propertyName: yellow-power-status
collectCycle: 10000000000 #Mapper 从设备收集数据的频率
reportCycle: 1000000000 #Mapper 报告数据的频率
customizedProtocol:
protocolName: GPIO
configData:
pin: 3 #pls give your yellow light's Pin, pin 2 is not vaild for atlas200dk

status:
twins:
- propertyName: red-power-status
reported:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
desired:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
- propertyName: green-power-status
reported:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
desired:
metadata:
timestamp: '1550049403598'
type: string
value: "ON"
- propertyName: yellow-power-status
reported:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
desired:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"

26 changes: 26 additions & 0 deletions mappers/gpio-atlas200/crd_example/gpio-device-model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: devices.kubeedge.io/v1alpha2
kind: DeviceModel
metadata:
name: led-model
namespace: default
spec:
properties:
- name: red-power-status
description: red
type:
string:
accessMode: ReadWrite
defaultValue: "OFF"
- name: green-power-status
description: green
type:
string:
accessMode: ReadWrite
defaultValue: "OFF"
- name: yellow-power-status
description: yellow
type:
string:
accessMode: ReadWrite
defaultValue: "ON"

131 changes: 131 additions & 0 deletions mappers/gpio-atlas200/driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package driver

import (
"encoding/json"
"fmt"
"strings"
"sync"
)

// GPIOProtocolConfig is the protocol config structure.
type GPIOProtocolConfig struct {
ProtocolName string `json:"protocolName"`
ProtocolConfigData `json:"configData"`
}
// ProtocolConfigData is the protocol config data structure.
type ProtocolConfigData struct {
}

// GPIOProtocolCommonConfig is the protocol common config structure.
type GPIOProtocolCommonConfig struct {
CommonCustomizedValues `json:"customizedValues"`
}

// CommonCustomizedValues is the customized values structure.
type CommonCustomizedValues struct {
}
// GPIOVisitorConfig is the visitor config structure.
type GPIOVisitorConfig struct {
ProtocolName string `json:"protocolName"`
VisitorConfigData `json:"configData"`
}

// VisitorConfigData is the visitor config data structure.
type VisitorConfigData struct {
Pin int `json:"pin"`
}

// GPIO Realize the structure of random number
type GPIO struct {
mutex sync.Mutex
protocolConfig GPIOProtocolConfig
protocolCommonConfig GPIOProtocolCommonConfig
visitorConfig GPIOVisitorConfig
}

// InitDevice Sth that need to do in the first
// If you need mount a persistent connection, you should provide parameters in configmap's protocolCommon.
// and handle these parameters in the following function
func (d *GPIO) InitDevice(protocolCommon []byte) (err error) {
if protocolCommon != nil {
if err = json.Unmarshal(protocolCommon, &d.protocolCommonConfig); err != nil {
fmt.Printf("Unmarshal ProtocolCommonConfig error: %v\n", err)
return err
}
}
fmt.Println("GPIO devices do not need to be initialized")
return nil
}

// SetConfig Parse the configmap's raw json message
func (d *GPIO) SetConfig(protocolCommon, visitor, protocol []byte) (pin int, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()
if protocolCommon != nil {
if err = json.Unmarshal(protocolCommon, &d.protocolCommonConfig); err != nil {
fmt.Printf("Unmarshal ProtocolCommonConfig error: %v\n", err)
return 0, err
}
}
if visitor != nil {
if err = json.Unmarshal(visitor, &d.visitorConfig); err != nil {
fmt.Printf("Unmarshal visitorConfig error: %v\n", err)
return 0, err
}
}
if protocol != nil {
if err = json.Unmarshal(protocol, &d.protocolConfig); err != nil {
fmt.Printf("Unmarshal ProtocolConfig error: %v\n", err)
return 0, err
}
}
return d.visitorConfig.Pin, nil
}

// ReadDeviceData is an interface that reads data from a specific device, data is a type of string
func (d *GPIO) ReadDeviceData(protocolCommon, visitor, protocol []byte) (data interface{}, err error) {
// Parse raw json message to get a virtualDevice instance
pin, err := d.SetConfig(protocolCommon, visitor, protocol)
if err != nil {
return nil, err
}
pinClient := Pin(pin)
if pinClient.Read() == '0' {
return "OFF", nil
}
return "ON", nil
}

// WriteDeviceData is an interface that write data to a specific device, data's DataType is Consistent with configmap
func (d *GPIO) WriteDeviceData(data interface{}, protocolCommon, visitor, protocol []byte) (err error) {
// Parse raw json message to get a virtualDevice instance
pin, err := d.SetConfig(protocolCommon, visitor, protocol)
if err != nil {
return err
}
status := data.(string)
pinClient := Pin(pin)
if strings.ToUpper(status) == "OFF" {
pinClient.SetOutPut()
pinClient.SetLow()
} else if strings.ToUpper(status) == "ON" {
pinClient.SetOutPut()
pinClient.SetHight()
} else {
fmt.Println("the command should be \"ON\" or \"OFF\"")
}
return nil
}

// StopDevice is an interface to disconnect a specific device
// This function is called when mapper stops serving
func (d *GPIO) StopDevice() (err error) {
// in this func, u can get ur device-instance in the client map, and give a safety exit
fmt.Println("----------Stop gpio Device Successful----------")
return nil
}

// GetDeviceStatus is an interface to get the device status true is OK , false is DISCONNECTED
func (d *GPIO) GetDeviceStatus(protocolCommon, visitor, protocol []byte) (status bool) {
return true
}
Loading