slcansvc

package module
v0.0.0-...-6ba3d5a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 22 Imported by: 0

README

Serial-Line CAN Microservice
############################

Overview
########

``slcan-svc`` is a `go-kit <https://github.com/go-kit/kit>`_ based microserivce impelementation
which is capable of bridging serial-line CAN communication via RESTful APIs. The serial-line
backend of the service is partially `Serial-Line CAN <https://github.com/torvalds/linux/blob/master/drivers/net/can/slcan/slcan-core.c>`_
compliant for interfacing a `slcan <https://github.com/jonathanyhliang/zephyr/tree/slcan/samples/subsys/canbus/slcan>`_ end device.

``slcan-svc`` could interact with `mcumgr-svc <https://github.com/jonathanyhliang/mcumgr-svc>`_ using ``RabbitMQ`` to perform
``Device Firmware Update``. See `demo-svc <https://github.com/jonathanyhliang/demo-svc>`_ for the detail.

Building and Running
####################

Clone and build ``slcan-svc`` repository:

.. code-block:: console

        cd /workdir/slcan-svc
        go build -o ./build/cli ./cli

Get **slcan-svc** usage:

.. code-block:: console

        ./workdir/build/cli -h

         Usage of /workdir/slcan-svc/build/cli:
        -a string
                HTTP listen address (default ":8080")
        -b int
                SLCAN port baudrate (default 115200)
        -p string
                SLCAN port
        -u string
                AMQP dialing address (default "amqp://guest:guest@localhost:5672/")


Run **slcan-svc** with serial port specified:

.. code-block:: console

       ./workdir/build/cli -p /dev/ttyACM0

To access the RESTful APIs:

.. code-block:: console

        curl http://localhost:8080/slcan \
                --include --header "Content-Type: application/json" \
                --request "POST" \
                --data "{"id": 123, "data": "200rpm"}"
        
        curl http://localhost:8080/slcan/123 \
                --include --header "Content-Type: application/json" \
                --request "GET"

        curl http://localhost:8080/slcan/123 \
                --include --header "Content-Type: application/json" \
                --request "PUT" \
                --data "{"id": 123, "data": "300rpm"}"
        
        curl http://localhost:8080/slcan/123 \
                --include --header "Content-Type: application/json" \
                --request "DELETE"

Documentation

Index

Constants

View Source
const (
	CAN_ID_MIN = 0x0
	CAN_ID_MAX = 0x1fffffff
)

Variables

View Source
var (
	ErrBackendPortOpen     = errors.New("Backend: port open error")
	ErrBackendPortClose    = errors.New("Backend: port close error")
	ErrBackendPortFlush    = errors.New("Backend: port flush error")
	ErrBackendSlcanInit    = errors.New("Backend: SLCAN initialise error")
	ErrBackendInvalidID    = errors.New("Backend: invalid ID")
	ErrBackendInvalidData  = errors.New("Backend: invalid data")
	ErrBackendInvalidFrame = errors.New("Backend: invalid frame")
	ErrBackendReboot       = errors.New("Backend: reboot failed")
	ErrBackendOnhold       = errors.New("Backend: on hold")
	ErrBackendMsgQueue     = errors.New("Backend: message queue ping failed")
)
View Source
var (
	ErrDatabaseAlreadyExists = errors.New("Database: already exists")
	ErrDatabaseNotFound      = errors.New("Database: request not found")
)
View Source
var (
	ErrServiceInvalidID = errors.New("Service: invalid id")
)
View Source
var (
	// ErrTransportBadRouting is returned when an expected path variable is missing.
	// It always indicates programmer error.
	ErrTransportBadRouting = errors.New("Transport: bad routing")
)

Functions

func DecodeDeleteMessageRequest

func DecodeDeleteMessageRequest(_ context.Context, r *http.Request) (request interface{}, err error)

func DecodeDeleteMessageResponse

func DecodeDeleteMessageResponse(_ context.Context, r *http.Response) (interface{}, error)

func DecodeGetMessageRequest

func DecodeGetMessageRequest(_ context.Context, r *http.Request) (request interface{}, err error)

func DecodeGetMessageResponse

func DecodeGetMessageResponse(_ context.Context, r *http.Response) (interface{}, error)

func DecodePostMessageRequest

func DecodePostMessageRequest(_ context.Context, r *http.Request) (request interface{}, err error)

func DecodePostMessageResponse

func DecodePostMessageResponse(_ context.Context, r *http.Response) (interface{}, error)

func DecodePutMessageRequest

func DecodePutMessageRequest(_ context.Context, r *http.Request) (request interface{}, err error)

func DecodePutMessageResponse

func DecodePutMessageResponse(_ context.Context, r *http.Response) (interface{}, error)

func DecodeRebootRequest

func DecodeRebootRequest(_ context.Context, r *http.Request) (request interface{}, err error)

func DecodeRebootResponse

func DecodeRebootResponse(_ context.Context, r *http.Response) (interface{}, error)

func DecodeUnlockRequest

func DecodeUnlockRequest(_ context.Context, r *http.Request) (request interface{}, err error)

func DecodeUnlockResponse

func DecodeUnlockResponse(_ context.Context, r *http.Response) (interface{}, error)

func EncodeDeleteMessageRequest

func EncodeDeleteMessageRequest(ctx context.Context, req *http.Request, request interface{}) error

func EncodeGetMessageRequest

func EncodeGetMessageRequest(ctx context.Context, req *http.Request, request interface{}) error

func EncodePostMessageRequest

func EncodePostMessageRequest(ctx context.Context, req *http.Request, request interface{}) error

func EncodePutMessageRequest

func EncodePutMessageRequest(ctx context.Context, req *http.Request, request interface{}) error

func EncodeRebootRequest

func EncodeRebootRequest(ctx context.Context, req *http.Request, request interface{}) error

func EncodeResponse

func EncodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error

func EncodeUnlockRequest

func EncodeUnlockRequest(ctx context.Context, req *http.Request, request interface{}) error

func MakeDeleteMessageEndpoint

func MakeDeleteMessageEndpoint(s IService) endpoint.Endpoint

func MakeGetMessageEndpoint

func MakeGetMessageEndpoint(s IService) endpoint.Endpoint

func MakeHTTPHandler

func MakeHTTPHandler(s IService, logger log.Logger) http.Handler

func MakePostMessageEndpoint

func MakePostMessageEndpoint(s IService) endpoint.Endpoint

func MakePutMessageEndpoint

func MakePutMessageEndpoint(s IService) endpoint.Endpoint

func MakeRebootEndpoint

func MakeRebootEndpoint(s IService) endpoint.Endpoint

func MakeUnlockEndpoint

func MakeUnlockEndpoint(s IService) endpoint.Endpoint

Types

type Backend

type Backend struct {
	// contains filtered or unexported fields
}

func (*Backend) GetMessage

func (b *Backend) GetMessage(id int) error

func (*Backend) Handler

func (b *Backend) Handler(port string, baud int, url string) error

func (*Backend) PostMessage

func (b *Backend) PostMessage(m Message) error

func (*Backend) Reboot

func (b *Backend) Reboot() error

func (*Backend) Unlock

func (b *Backend) Unlock() error

type Database

type Database struct {
	// contains filtered or unexported fields
}

func (*Database) DeleteData

func (d *Database) DeleteData(id uint32) error

func (*Database) GetData

func (d *Database) GetData(id uint32) (Message, error)

func (*Database) PostData

func (d *Database) PostData(m Message) error

func (*Database) PutData

func (d *Database) PutData(id uint32, m Message) error

func (*Database) WriteData

func (d *Database) WriteData(m Message) error

type Endpoints

type Endpoints struct {
	GetMessageEndpoint    endpoint.Endpoint
	PostMessageEndpoint   endpoint.Endpoint
	PutMessageEndpoint    endpoint.Endpoint
	DeleteMessageEndpoint endpoint.Endpoint
	RebootEndpoint        endpoint.Endpoint
	UnlockEndpoint        endpoint.Endpoint
}

func MakeClientEndpoints

func MakeClientEndpoints(instance string) (Endpoints, error)

MakeClientEndpoints returns an Endpoints struct where each endpoint invokes the corresponding method on the remote instance, via a transport/http.Client.

func MakeServerEndpoints

func MakeServerEndpoints(s IService) Endpoints

func (Endpoints) DeleteMessage

func (e Endpoints) DeleteMessage(ctx context.Context, id int) error

func (Endpoints) GetMessage

func (e Endpoints) GetMessage(ctx context.Context, id int) (Message, error)

struct Endpoints implements interface IService. Primarily useful in a client.

func (Endpoints) PostMessage

func (e Endpoints) PostMessage(ctx context.Context, m Message) error

func (Endpoints) PutMessage

func (e Endpoints) PutMessage(ctx context.Context, id int, m Message) error

func (Endpoints) Reboot

func (e Endpoints) Reboot(ctx context.Context) error

func (Endpoints) Unlock

func (e Endpoints) Unlock(ctx context.Context) error

type IBackend

type IBackend interface {
	Handler(port string, baud int, url string) error
	GetMessage(id int) error
	PostMessage(m Message) error
	Reboot() error
	Unlock() error
}

func NewBackend

func NewBackend() IBackend

type IService

type IService interface {
	GetMessage(ctx context.Context, id int) (Message, error)
	PostMessage(ctx context.Context, m Message) error
	PutMessage(ctx context.Context, id int, m Message) error
	DeleteMessage(ctx context.Context, id int) error
	Reboot(ctx context.Context) error
	Unlock(ctx context.Context) error
}

func NewService

func NewService() IService

type Message

type Message struct {
	ID   uint32 `json:"id" example:"123"`
	Data string `json:"data" example:"200rpm"`
}

type Middleware

type Middleware func(IService) IService

Middleware describes a service (as opposed to endpoint) middleware.

func BackendMiddleware

func BackendMiddleware(backend IBackend) Middleware

func LoggingMiddleware

func LoggingMiddleware(logger log.Logger) Middleware

type Service

type Service struct{}

func (*Service) DeleteMessage

func (s *Service) DeleteMessage(ctx context.Context, id int) error

DeleteMessage godoc

@Summary	Remove CAN message
@Schemes
@Description	Remove CAN message by specifying CAN ID
@Tags			SLCAN
@Param			int	path	int	true	"CAN ID"	minimum(0)	maximum(536870911)
@Accept			json
@Produce		json
@Success		200
@Failure		400
@Failure		404
@Failure		500
@Router			/slcan/{id} [delete]

func (*Service) GetMessage

func (s *Service) GetMessage(ctx context.Context, id int) (Message, error)

GetMessage godoc

@Summary	Retrieve CAN message
@Schemes
@Description	Retrieve CAN message by specifying CAN ID
@Tags			SLCAN
@Param			int	path	int	true	"CAN ID"	minimum(0)	maximum(536870911)
@Accept			json
@Produce		json
@Success		200	{array}	slcansvc.Message
@Failure		400
@Failure		404
@Failure		500
@Router			/slcan/{id} [get]

func (*Service) PostMessage

func (s *Service) PostMessage(ctx context.Context, m Message) error

PostMessage godoc

@Summary	Add new CAN message
@Schemes
@Description	Add new CAN message by specifying CAN ID and data
@Tags			SLCAN
@Param			array	body	slcansvc.Message	false	"CAN Message"
@Accept			json
@Produce		json
@Success		200
@Failure		400
@Failure		404
@Failure		500
@Router			/slcan [post]

func (*Service) PutMessage

func (s *Service) PutMessage(ctx context.Context, id int, m Message) error

PutMessage godoc

@Summary	Update existing CAN message
@Schemes
@Description	Update existing CAN message by specifying CAN ID and data
@Tags			SLCAN
@Param			array	body	slcansvc.Message	false	"CAN Message"
@Accept			json
@Produce		json
@Success		200
@Failure		400
@Failure		404
@Failure		500
@Router			/slcan/{id} [put]

func (*Service) Reboot

func (s *Service) Reboot(ctx context.Context) error

Reboot godoc

@Summary	Reboot SLCAN device
@Schemes
@Description	Reboot SLCAN device for firmware update
@Tags			SLCAN
@Accept			json
@Produce		json
@Success		200
@Failure		400
@Failure		404
@Failure		500
@Router			/slcan/reboot [post]

func (*Service) Unlock

func (s *Service) Unlock(ctx context.Context) error

Unlock godoc

@Summary	Unlock serial backend
@Schemes
@Description	Unlock serial backend from the success of firmware update
@Tags			SLCAN
@Accept			json
@Produce		json
@Success		200
@Failure		400
@Failure		404
@Failure		500
@Router			/slcan/unlock [post]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL