LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
candriver.h
Go to the documentation of this file.
1#pragma once
3#define NO_SUBITEM 63
5#define SUBITEM_IS_COMMAND 0x20
6#ifdef CANDRV
7
8#if defined(ARDUINO_ARCH_STM32)
9#if !defined(HAL_CAN_MODULE_ENABLED)
10#define HAL_CAN_MODULE_ENABLED
11#endif
12#include <STM32_CAN.h>
13#endif
14
15#include <itemCmd.h>
16
17#include <Stream.h>
18#include <aJSON.h>
19#include <streamlog.h>
20//#include <config.h> NO!
21
23typedef uint8_t macAddress[6];
24
36#pragma pack(push, 1)
37typedef union
38{
39 uint32_t id;
40 struct
41 { union
42 {
43 struct
44 {
45 uint16_t subItemId:6;
46 uint16_t itemId:10;
47 };
48 uint16_t subjId;
49 };
50 uint8_t deviceId;
51 uint8_t payloadType:4;
52 uint8_t status:1;
53 uint8_t reserve:3; //0
54
55 };
56} canid_t;
57
59enum payloadType
60{ unknown=0,
61 itemCommand=1,
62 lookupMAC=2,
63 configFrame=3,
64 OTAFrame=4,
65 auth=5,
66 metric=6,
67 sysCmd=7,
68 rawPinCtrl=8
69};
70
72enum metricType
73{
74 MAC=1,
75 IP=2,
76 NetMask=3,
77 GW=4,
78 DNS=5,
79 UpTime=6,
80 Salt=7
81};
82
84enum commandType
85{
86 reboot=1,
87 get=2,
88 save=3,
89 load=4
90};
91
92
93#define MAXCANID 0x1FFFFFFF
94// Request item status: id.status=1;deviceId=[0xFF | deviceId];itemId=x; RTR bit=true
95// Request on config: id.status=0;deviceId=0;itemId=x payload.mac=mac;crc16=crc_current_config
96
97
98
105typedef union {
106 uint8_t data[8];
107 char payload[8];
108 struct {
109 itemCmdStore cmd;
110 itemArgStore param;
111 };
112 struct {
113 macAddress mac;
114 uint16_t currentConfCRC;
115 };
116 struct {
117 uint8_t sysCmd;
118 uint8_t sysCmdData[7];
119 };
120 struct {
121 uint32_t metric1;
122 uint32_t metric2;
123 };
124} datagram_t;
125
127enum canState
128{
129stateUnknown,
130MACLookup,
131Idle,
132StreamOpenedWrite,
133StreamOpenedRead,
134FrameRequested,
135FrameReceived,
136ReadConfig,
137ConfigLoaded,
138waitingConfirm,
139Error
140};
141#pragma pack(pop)
142
154class canDriver
155{
156public:
158canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::stateUnknown;canConfigObj=NULL;canRemoteConfigObj=NULL;confCRC=0xFFFF;};
163uint8_t getMyId();
171bool sendStatus(uint16_t itemNum, itemCmd cmd, int subItem = NO_SUBITEM);
181bool sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool status=false, int subItemID=NO_SUBITEM );
190bool sendCommand(aJsonObject * can,itemCmd cmd, bool status = false);
196bool upTime(uint32_t ut);
202bool salt(uint32_t salt);
208bool lookupMAC();
216bool requestFrame(uint8_t devId, payloadType _payloadType, uint16_t seqNo );
221int readFrame();
227bool sendRemoteID(macAddress mac);
233bool begin();
238void Poll();
247bool processPacket(canid_t id, datagram_t *packet, uint8_t len, bool rtr=false);
255bool write(uint32_t msg_id, datagram_t * buf = NULL, uint8_t size=0);
262aJsonObject * findConfbyName(char* devName, int * devAddr=NULL);
263#if not defined (NOIP)
271bool subscribeTopics(char * root, size_t buflen);
272#endif
273
278uint8_t getControllerID(){return controllerId;};
284uint8_t getIdByMac(macAddress mac);
286aJsonObject * canConfigObj;
288aJsonObject * canRemoteConfigObj;
290uint16_t confCRC;
292 datagram_t RXpacket;
294 canid_t RXid;
296 uint8_t RXlen;
297
298private:
304 aJsonObject * getConfbyID(uint8_t devId);
305
306 #if defined(ARDUINO_ARCH_STM32)
307 CAN_message_t CAN_RX_msg;
308 CAN_message_t CAN_TX_msg;
309 #endif
310
311 #if defined(__SAM3X8E__)
312 //CAN_FRAME CAN_RX_msg;
313 #endif
314
316 bool ready;
317
319 uint8_t controllerId;
321 canState state;
323 uint32_t responseTimer;
324
325};
326
327
328
330extern aJsonObject * topics;
331
342class canStream : public Stream
343{
344public:
349 canStream(canDriver * _driver) : readPos(0),writePos(0),devId(0), pType(payloadType::unknown),state(canState::stateUnknown),seqNo(0),failedCount(0){driver=_driver; }
357 int open(uint8_t controllerID, payloadType _pType, char _mode)
358 {
359 if (mode) close();
360 devId=controllerID;
361 pType = _pType;
362 mode = _mode;
363 seqNo=0xFFFF;
364 failedCount=0;
365 if (mode == 'w') state=canState::StreamOpenedWrite;
366 else state=canState::StreamOpenedRead;
367 return 1;
368 };
373 int close ()
374 {
375 if ((mode == 'w') && writePos) flush();
376 mode = '\0';
377 state=canState::stateUnknown;
378 return 1;
379 }
380
381
382 // Stream methods
387 virtual int available();
392 virtual int read();
397 virtual int peek();
398
402 virtual void flush();
403 // Print methods
409 virtual size_t write(uint8_t c) ;
414 virtual int availableForWrite();
415
416
417
418private:
425 int send(uint8_t len, uint16_t _seqNo);
431 int checkState();
433 canDriver * driver;
435 unsigned int readPos;
437 unsigned int writePos;
438
440 datagram_t writeBuffer;
441
443 uint8_t devId;
445 uint16_t seqNo;
447 int8_t failedCount;
449 char mode;
451 payloadType pType;
453 canState state;
454 //bool writeBlocked;
455};
456
457
458
459
460
461
462#endif //
#define NO_SUBITEM
Sub-item index meaning "no sub-item" (top of 6-bit field).
Definition candriver.h:3
aJsonObject * root
Definition main.cpp:119
aJsonObject * topics
Definition main.cpp:118
uint8_t macAddress[6]
6-byte MAC address.
Definition systemconfigdata.h:38
32-bit parameter storage of itemCmd.
Definition itemCmd.h:267
Packed 32-bit command word of itemCmd.
Definition itemCmd.h:245