link_layer.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  1. #include "iec_include.h"
  2. #include "link_layer.h"
  3. #include "lib_memory.h"
  4. #include "link_layer_private.h"
  5. typedef struct sLinkLayerSlaveConnection* LinkLayerSlaveConnection;
  6. typedef struct sLinkLayerSecondaryBalanced* LinkLayerSecondaryBalanced;
  7. typedef struct sLinkLayerPrimaryBalanced* LinkLayerPrimaryBalanced;
  8. typedef struct sLinkLayerSecondaryUnbalanced* LL_Sec_Unb; /* short cut definition */
  9. struct sLinkLayerSecondaryBalanced {
  10. bool expectedFcb; /* expected value of next frame count bit (FCB) */
  11. LinkLayer linkLayer;
  12. IBalancedApplicationLayer applicationLayer;
  13. void* appLayerParam;
  14. };
  15. typedef enum {
  16. PLL_IDLE,
  17. PLL_EXECUTE_REQUEST_STATUS_OF_LINK,
  18. PLL_EXECUTE_RESET_REMOTE_LINK,
  19. PLL_LINK_LAYERS_AVAILABLE,
  20. PLL_EXECUTE_SERVICE_SEND_CONFIRM,
  21. PLL_EXECUTE_SERVICE_REQUEST_RESPOND,
  22. PLL_SECONDARY_LINK_LAYER_BUSY, /* Only required in balanced link layer */
  23. PLL_TIMEOUT /* only required in unbalanced link layer */
  24. } PrimaryLinkLayerState;
  25. struct sLinkLayerPrimaryBalanced {
  26. LinkLayerState state; /* state information for user */
  27. PrimaryLinkLayerState primaryState; /* internal PLL state machine state */
  28. bool waitingForResponse;
  29. uint64_t lastSendTime;
  30. uint64_t originalSendTime;
  31. bool sendLinkLayerTestFunction;
  32. bool nextFcb;
  33. int otherStationAddress;
  34. struct sBufferFrame lastSendAsdu;
  35. IBalancedApplicationLayer applicationLayer;
  36. void* applicationLayerParam;
  37. uint64_t lastReceivedMsg;
  38. int idleTimeout; /* connection timeout in ms */
  39. IEC60870_LinkLayerStateChangedHandler stateChangedHandler;
  40. void* stateChangedHandlerParameter;
  41. LinkLayer linkLayer;
  42. };
  43. struct sLinkLayerSlaveConnection {
  44. LinkLayerPrimaryUnbalanced primaryLink;
  45. LinkLayerState state; /* user visible state */
  46. int address;
  47. PrimaryLinkLayerState primaryState; /* internal PLL state machine state */
  48. bool hasMessageToSend; /* indicates if an app layer message is ready to be sent */
  49. struct sBufferFrame nextMessage;
  50. uint8_t buffer[256];
  51. uint64_t lastSendTime;
  52. uint64_t originalSendTime;
  53. bool requestClass1Data;
  54. bool requestClass2Data;
  55. bool dontSendMessages;
  56. bool waitingForResponse;
  57. bool sendLinkLayerTestFunction;
  58. bool nextFcb;
  59. };
  60. struct sLinkLayer {
  61. uint8_t buffer[261]; /* 261 = maximum FT1.2 frame length */
  62. uint8_t userDataBuffer[255];
  63. int address;
  64. SerialTransceiverFT12 transceiver;
  65. LinkLayerParameters linkLayerParameters;
  66. bool dir; /* DIR bit to be used when messages are sent in balanced mode */
  67. LinkLayerSecondaryUnbalanced llSecUnbalanced;
  68. LinkLayerSecondaryBalanced llSecBalanced;
  69. LinkLayerPrimaryBalanced llPriBalanced;
  70. LinkLayerPrimaryUnbalanced llPriUnbalanced;
  71. };
  72. struct sLinkLayerSecondaryUnbalanced {
  73. /**
  74. * Initialized/timeout -> state = IDLE
  75. * received invalid message -> state = ERROR
  76. * received valid message/no timeout -> state = AVAILABLE
  77. */
  78. LinkLayerState state;
  79. bool expectedFcb; /* expected value of next frame count bit (FCB) */
  80. LinkLayer linkLayer;
  81. ISecondaryApplicationLayer applicationLayer;
  82. void* appLayerParam;
  83. LinkLayerParameters linkLayerParameters;
  84. uint64_t lastReceivedMsg;
  85. int idleTimeout; /* connection timeout in ms */
  86. IEC60870_LinkLayerStateChangedHandler stateChangedHandler;
  87. void* stateChangedHandlerParameter;
  88. struct sLinkLayer _linkLayer;
  89. };
  90. struct sLinkLayerPrimaryUnbalanced {
  91. LinkLayerSlaveConnection currentSlave;
  92. int currentSlaveIndex;
  93. bool hasNextBroadcastToSend;
  94. struct sBufferFrame nextBroadcastMessage;
  95. uint8_t buffer[256];
  96. IPrimaryApplicationLayer applicationLayer;
  97. void* applicationLayerParam;
  98. struct sLinkLayer _linkLayer;
  99. LinkLayer linkLayer;
  100. LinkedList slaveConnections;
  101. IEC60870_LinkLayerStateChangedHandler stateChangedHandler;
  102. void* stateChangedHandlerParameter;
  103. };
  104. struct sLinkLayerBalanced {
  105. LinkLayer linkLayer;
  106. IBalancedApplicationLayer applicationLayer;
  107. void* appLayerParameter;
  108. struct sLinkLayer _linkLayer;
  109. struct sLinkLayerPrimaryBalanced primaryLinkLayer;
  110. struct sLinkLayerSecondaryBalanced secondaryLinkLayer;
  111. };
  112. bool
  113. IMasterConnection_sendACT_CON(IMasterConnection self, CS101_ASDU asdu, bool negative)
  114. {
  115. return self->sendACT_CON(self, asdu, negative);
  116. }
  117. CS101_AppLayerParameters
  118. IMasterConnection_getApplicationLayerParameters(IMasterConnection self)
  119. {
  120. return self->getApplicationLayerParameters(self);
  121. }
  122. bool
  123. IMasterConnection_sendASDU(IMasterConnection self, CS101_ASDU asdu)
  124. {
  125. return self->sendASDU(self, asdu);
  126. }
  127. bool
  128. IMasterConnection_sendACT_TERM(IMasterConnection self, CS101_ASDU asdu)
  129. {
  130. return self->sendACT_TERM(self, asdu);
  131. }
  132. LinkLayer
  133. LinkLayer_init(LinkLayer self, int address, SerialTransceiverFT12 transceiver, LinkLayerParameters linkLayerParameters)
  134. {
  135. if (self == NULL)
  136. self = (LinkLayer) GLOBAL_MALLOC(sizeof(struct sLinkLayer));
  137. if (self != NULL) {
  138. self->address = address;
  139. self->transceiver = transceiver;
  140. self->linkLayerParameters = linkLayerParameters;
  141. self->dir = false;
  142. self->llSecUnbalanced = NULL;
  143. self->llSecBalanced = NULL;
  144. self->llPriBalanced = NULL;
  145. self->llPriUnbalanced = NULL;
  146. }
  147. return self;
  148. }
  149. LinkLayerPrimaryUnbalanced
  150. LinkLayerPrimaryUnbalanced_create(SerialTransceiverFT12 transceiver, LinkLayerParameters linkLayerParameters,
  151. IPrimaryApplicationLayer applicationLayer, void* applicationLayerParam)
  152. {
  153. LinkLayerPrimaryUnbalanced self = (LinkLayerPrimaryUnbalanced) GLOBAL_MALLOC(sizeof(struct sLinkLayerPrimaryUnbalanced));
  154. if (self) {
  155. self->currentSlave = NULL;
  156. self->currentSlaveIndex = 0;
  157. self->hasNextBroadcastToSend = false;
  158. self->applicationLayer = applicationLayer;
  159. self->applicationLayerParam = applicationLayerParam;
  160. self->linkLayer = &(self->_linkLayer);
  161. LinkLayer_init(self->linkLayer, 0, transceiver, linkLayerParameters);
  162. self->linkLayer->llPriUnbalanced = self;
  163. BufferFrame_initialize(&(self->nextBroadcastMessage), self->buffer, 0);
  164. self->slaveConnections = LinkedList_create();
  165. self->stateChangedHandler = NULL;
  166. }
  167. return self;
  168. }
  169. static void
  170. LinkLayerPrimaryBalanced_init(LinkLayerPrimaryBalanced self, LinkLayer linkLayer, IBalancedApplicationLayer applicationLayer, void* appLayerParam)
  171. {
  172. self->primaryState = PLL_IDLE;
  173. self->state = LL_STATE_IDLE;
  174. self->waitingForResponse = false;
  175. self->sendLinkLayerTestFunction = false;
  176. self->nextFcb = true;
  177. self->linkLayer = linkLayer;
  178. self->applicationLayer = applicationLayer;
  179. self->applicationLayerParam = appLayerParam;
  180. self->stateChangedHandler = NULL;
  181. self->idleTimeout = 5000;
  182. self->otherStationAddress = 0;
  183. }
  184. static void
  185. LinkLayerSecondaryBalanced_init(LinkLayerSecondaryBalanced self, LinkLayer linkLayer, IBalancedApplicationLayer applicationLayer, void* appLayerParam)
  186. {
  187. self->expectedFcb = true;
  188. self->linkLayer = linkLayer;
  189. self->applicationLayer = applicationLayer;
  190. self->appLayerParam = appLayerParam;
  191. }
  192. LinkLayerBalanced
  193. LinkLayerBalanced_create(
  194. int linkLayerAddress,
  195. SerialTransceiverFT12 transceiver,
  196. LinkLayerParameters linkLayerParameters,
  197. IBalancedApplicationLayer applicationLayer,
  198. void* applicationLayerParameter
  199. )
  200. {
  201. LinkLayerBalanced self = (LinkLayerBalanced) GLOBAL_MALLOC(sizeof(struct sLinkLayerBalanced));
  202. if (self) {
  203. self->linkLayer = LinkLayer_init(&(self->_linkLayer), linkLayerAddress, transceiver, linkLayerParameters);
  204. self->applicationLayer = applicationLayer;
  205. self->appLayerParameter = applicationLayerParameter;
  206. LinkLayerPrimaryBalanced_init(&(self->primaryLinkLayer), self->linkLayer, applicationLayer, applicationLayerParameter);
  207. LinkLayerSecondaryBalanced_init(&(self->secondaryLinkLayer), self->linkLayer, applicationLayer, applicationLayerParameter);
  208. self->linkLayer->llPriBalanced = &(self->primaryLinkLayer);
  209. self->linkLayer->llSecBalanced = &(self->secondaryLinkLayer);
  210. }
  211. return self;
  212. }
  213. void
  214. LinkLayerBalanced_setIdleTimeout(LinkLayerBalanced self, int timeoutInMs)
  215. {
  216. self->primaryLinkLayer.idleTimeout = timeoutInMs;
  217. }
  218. void
  219. LinkLayerBalanced_setDIR(LinkLayerBalanced self, bool dir)
  220. {
  221. self->linkLayer->dir = dir;
  222. }
  223. void
  224. LinkLayerBalanced_setStateChangeHandler(LinkLayerBalanced self,
  225. IEC60870_LinkLayerStateChangedHandler handler, void* parameter)
  226. {
  227. self->primaryLinkLayer.stateChangedHandler = handler;
  228. self->primaryLinkLayer.stateChangedHandlerParameter = parameter;
  229. }
  230. void
  231. LinkLayerPrimaryUnbalanced_setStateChangeHandler(LinkLayerPrimaryUnbalanced self,
  232. IEC60870_LinkLayerStateChangedHandler handler, void* parameter)
  233. {
  234. self->stateChangedHandler = handler;
  235. self->stateChangedHandlerParameter = parameter;
  236. }
  237. static LinkLayerSlaveConnection
  238. LinkLayerPrimaryUnbalanced_getSlaveConnection(LinkLayerPrimaryUnbalanced self, int slaveAddress)
  239. {
  240. LinkLayerSlaveConnection retVal = NULL;
  241. LinkedList element = LinkedList_getNext(self->slaveConnections);
  242. while (element) {
  243. LinkLayerSlaveConnection slaveConnection = (LinkLayerSlaveConnection) LinkedList_getData(element);
  244. if (slaveConnection->address == slaveAddress) {
  245. retVal = slaveConnection;
  246. break;
  247. }
  248. element = LinkedList_getNext(element);
  249. }
  250. return retVal;
  251. }
  252. static LinkLayerSlaveConnection
  253. LinkLayerSlaveConnection_create(LinkLayerSlaveConnection self, LinkLayerPrimaryUnbalanced primaryLink, int slaveAddress)
  254. {
  255. if (self == NULL)
  256. self = (LinkLayerSlaveConnection) GLOBAL_MALLOC(sizeof(struct sLinkLayerSlaveConnection));
  257. if (self) {
  258. self->primaryLink = primaryLink;
  259. self->address = slaveAddress;
  260. self->state = LL_STATE_IDLE;
  261. self->lastSendTime = 0;
  262. self->originalSendTime = 0;
  263. self->nextFcb = true;
  264. self->waitingForResponse = false;
  265. self->primaryState = PLL_IDLE;
  266. self->hasMessageToSend = false;
  267. self->sendLinkLayerTestFunction = false;
  268. self->requestClass1Data = false;
  269. self->requestClass2Data = false;
  270. BufferFrame_initialize(&(self->nextMessage), self->buffer, 0);
  271. }
  272. return self;
  273. }
  274. void
  275. LinkLayerPrimaryUnbalanced_addSlaveConnection(LinkLayerPrimaryUnbalanced self, int slaveAddress)
  276. {
  277. LinkLayerSlaveConnection slaveConnection = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, slaveAddress);
  278. if (slaveConnection == NULL) {
  279. LinkLayerSlaveConnection newSlave = LinkLayerSlaveConnection_create(NULL, self, slaveAddress);
  280. LinkedList_add(self->slaveConnections, newSlave);
  281. }
  282. }
  283. bool
  284. LinkLayerPrimaryUnbalanced_requestClass2Data(LinkLayerPrimaryUnbalanced self, int slaveAddress)
  285. {
  286. LinkLayerSlaveConnection slave = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, slaveAddress);
  287. if (slave) {
  288. slave->requestClass2Data = true;
  289. return true;
  290. }
  291. return false;
  292. }
  293. static bool
  294. LinkLayerSecondaryBalanced_checkFCB(LinkLayerSecondaryBalanced self, bool fcb)
  295. {
  296. if (fcb != self->expectedFcb) {
  297. printf ("ERROR: Frame count bit (FCB) invalid!\n");
  298. /* TODO change link status */
  299. return false;
  300. } else {
  301. self->expectedFcb = !(self->expectedFcb);
  302. return true;
  303. }
  304. }
  305. void
  306. LinkLayerSecondaryUnbalanced_setStateChangeHandler(LinkLayerSecondaryUnbalanced self,
  307. IEC60870_LinkLayerStateChangedHandler handler, void* parameter)
  308. {
  309. self->stateChangedHandler = handler;
  310. self->stateChangedHandlerParameter = parameter;
  311. }
  312. static void
  313. SendSingleCharCharacter(LinkLayer self)
  314. {
  315. uint8_t singleCharAck[] = {0xe5};
  316. SerialTransceiverFT12_sendMessage(self->transceiver, singleCharAck, 1);
  317. }
  318. static void
  319. SendFixedFrame(LinkLayer self, uint8_t fc, int address, bool prm, bool dir, bool acd /*FCB*/, bool dfc /*FCV*/)
  320. {
  321. uint8_t* buffer = self->buffer;
  322. int bufPos = 0;
  323. buffer[bufPos++] = 0x10; /* START */
  324. uint8_t c = fc & 0x0f;
  325. if (prm) c += 0x40;
  326. if (dir) c += 0x80;
  327. if (acd) c += 0x20;
  328. if (dfc) c += 0x10;
  329. buffer [bufPos++] = c;
  330. if (self->linkLayerParameters->addressLength > 0) {
  331. buffer [bufPos++] = (uint8_t) (address % 0x100);
  332. if (self->linkLayerParameters->addressLength > 1)
  333. buffer [bufPos++] = (uint8_t) ((address / 0x100) % 0x100);
  334. }
  335. uint8_t checksum = 0;
  336. int i;
  337. for (i = 1; i < bufPos; i++)
  338. checksum += buffer[i];
  339. buffer [bufPos++] = checksum;
  340. buffer [bufPos++] = 0x16; /* END */
  341. printf("Send fixed frame (fc=%i)\n", fc);
  342. SerialTransceiverFT12_sendMessage(self->transceiver, buffer, bufPos);
  343. }
  344. void
  345. LinkLayerSecondaryBalanced_handleMessage(LinkLayerSecondaryBalanced self, uint8_t fc, bool isBroadcast, bool fcb, bool fcv, uint8_t* msg, int userDataStart, int userDataLength)
  346. {
  347. if (fcv) {
  348. if (LinkLayerSecondaryBalanced_checkFCB(self, fcb) == false)
  349. return;
  350. }
  351. switch (fc) {
  352. case LL_FC_00_RESET_REMOTE_LINK:
  353. printf ("SLL - RECV FC 00 - RESET REMOTE LINK\n");
  354. self->expectedFcb = true;
  355. printf ("SLL - SEND FC 00 - ACK\n");
  356. if (self->linkLayer->linkLayerParameters->useSingleCharACK)
  357. SendSingleCharCharacter(self->linkLayer);
  358. else
  359. SendFixedFrame(self->linkLayer, LL_FC_00_ACK, self->linkLayer->address, false, self->linkLayer->dir, false, false);
  360. break;
  361. case LL_FC_02_TEST_FUNCTION_FOR_LINK:
  362. printf ("SLL - RECV FC 02 - TEST FUNCTION FOR LINK\n");
  363. printf ("SLL - SEND FC 00 - ACK\n");
  364. if (self->linkLayer->linkLayerParameters->useSingleCharACK)
  365. SendSingleCharCharacter(self->linkLayer);
  366. else
  367. SendFixedFrame(self->linkLayer, LL_FC_00_ACK, self->linkLayer->address, false, self->linkLayer->dir, false, false);
  368. break;
  369. case LL_FC_03_USER_DATA_CONFIRMED:
  370. printf("SLL - RECV FC 03 - USER DATA CONFIRMED\n");
  371. if (userDataLength > 0) {
  372. if (self->applicationLayer->HandleReceivedData(self->appLayerParam, msg, isBroadcast, userDataStart, userDataLength)) {
  373. printf ("SLL - SEND FC 00 - ACK\n");
  374. if (self->linkLayer->linkLayerParameters->useSingleCharACK)
  375. SendSingleCharCharacter(self->linkLayer);
  376. else
  377. SendFixedFrame(self->linkLayer, LL_FC_00_ACK, self->linkLayer->address, false, self->linkLayer->dir, false, false);
  378. }
  379. }
  380. break;
  381. case LL_FC_04_USER_DATA_NO_REPLY:
  382. printf ("SLL -FC 04 - USER DATA NO REPLY\n");
  383. if (userDataLength > 0) {
  384. self->applicationLayer->HandleReceivedData(self->appLayerParam, msg, isBroadcast, userDataStart, userDataLength);
  385. }
  386. break;
  387. case LL_FC_09_REQUEST_LINK_STATUS:
  388. printf ("SLL - RECV FC 09 - REQUEST LINK STATUS\n");
  389. printf ("SLL - SEND FC 11 - STATUS OF LINK\n");
  390. SendFixedFrame(self->linkLayer, LL_FC_11_STATUS_OF_LINK_OR_ACCESS_DEMAND, self->linkLayer->address, false, self->linkLayer->dir, false, false);
  391. break;
  392. default:
  393. printf ("SLL - UNEXPECTED LINK LAYER MESSAGE");
  394. printf ("SLL - SEND FC 15 - SERVICE NOT IMPLEMENTED\n");
  395. SendFixedFrame(self->linkLayer, LL_FC_15_SERVICE_NOT_IMPLEMENTED, self->linkLayer->address, false, self->linkLayer->dir, false, false);
  396. break;
  397. }
  398. }
  399. void
  400. LinkLayerPrimaryBalanced_resetIdleTimeout(LinkLayerPrimaryBalanced self)
  401. {
  402. self->lastReceivedMsg = Hal_getTimeInMs();
  403. }
  404. static void
  405. llpb_setNewState(LinkLayerPrimaryBalanced self, LinkLayerState newState)
  406. {
  407. if (newState != self->state) {
  408. self->state = newState;
  409. if (self->stateChangedHandler)
  410. self->stateChangedHandler(self->stateChangedHandlerParameter, -1, newState);
  411. }
  412. }
  413. void
  414. LinkLayerPrimaryBalanced_handleMessage(LinkLayerPrimaryBalanced self, uint8_t fc, bool dir, bool dfc, int address, uint8_t* msg, int userDataStart, int userDataLength)
  415. {
  416. UNUSED_PARAMETER(dir);
  417. UNUSED_PARAMETER(address);
  418. UNUSED_PARAMETER(msg);
  419. UNUSED_PARAMETER(userDataStart);
  420. UNUSED_PARAMETER(userDataLength);
  421. PrimaryLinkLayerState primaryState = self->primaryState;
  422. PrimaryLinkLayerState newState = primaryState;
  423. self->lastReceivedMsg = Hal_getTimeInMs();
  424. if (dfc) {
  425. switch (self->primaryState) {
  426. case PLL_EXECUTE_REQUEST_STATUS_OF_LINK:
  427. case PLL_EXECUTE_RESET_REMOTE_LINK:
  428. newState = PLL_EXECUTE_REQUEST_STATUS_OF_LINK;
  429. break;
  430. case PLL_EXECUTE_SERVICE_SEND_CONFIRM:
  431. case PLL_SECONDARY_LINK_LAYER_BUSY:
  432. newState = PLL_SECONDARY_LINK_LAYER_BUSY;
  433. break;
  434. default:
  435. break;
  436. }
  437. llpb_setNewState(self, LL_STATE_BUSY);
  438. self->primaryState = newState;
  439. return;
  440. }
  441. switch (fc) {
  442. case LL_FC_00_ACK:
  443. printf ("PLL - RECV FC 00 - ACK\n");
  444. if (primaryState == PLL_EXECUTE_RESET_REMOTE_LINK) {
  445. newState = PLL_LINK_LAYERS_AVAILABLE;
  446. llpb_setNewState(self, LL_STATE_AVAILABLE);
  447. self->waitingForResponse = false;
  448. }
  449. else if (primaryState == PLL_EXECUTE_SERVICE_SEND_CONFIRM) {
  450. if (self->sendLinkLayerTestFunction)
  451. self->sendLinkLayerTestFunction = false;
  452. newState = PLL_LINK_LAYERS_AVAILABLE;
  453. llpb_setNewState(self, LL_STATE_AVAILABLE);
  454. self->waitingForResponse = false;
  455. }
  456. else if (primaryState == PLL_EXECUTE_REQUEST_STATUS_OF_LINK) {
  457. /* stay in state and wait for response */
  458. printf ("ACK (FC 00) unexpected -> expected status-of-link (FC 11)\n");
  459. }
  460. else {
  461. self->waitingForResponse = false;
  462. }
  463. break;
  464. case LL_FC_01_NACK:
  465. printf ("PLL - RECV FC 01 - NACK\n");
  466. if (primaryState == PLL_EXECUTE_SERVICE_SEND_CONFIRM) {
  467. newState = PLL_SECONDARY_LINK_LAYER_BUSY;
  468. llpb_setNewState(self, LL_STATE_BUSY);
  469. }
  470. break;
  471. case LL_FC_08_RESP_USER_DATA:
  472. printf ("PLL - RECV FC 08 - RESP USER DATA\n");
  473. newState = PLL_IDLE;
  474. llpb_setNewState(self, LL_STATE_ERROR);
  475. break;
  476. case LL_FC_09_RESP_NACK_NO_DATA:
  477. printf ("PLL - RECV FC 09 - RESP NACK - NO DATA\n");
  478. newState = PLL_IDLE;
  479. llpb_setNewState(self, LL_STATE_ERROR);
  480. break;
  481. case LL_FC_11_STATUS_OF_LINK_OR_ACCESS_DEMAND:
  482. printf ("PLL - RECV FC 11 - STATUS OF LINK\n");
  483. if (primaryState == PLL_EXECUTE_REQUEST_STATUS_OF_LINK) {
  484. printf ("PLL - SEND FC 00 - RESET REMOTE LINK\n");
  485. SendFixedFrame(self->linkLayer, LL_FC_00_RESET_REMOTE_LINK, self->otherStationAddress, true, self->linkLayer->dir, false, false);
  486. self->lastSendTime = Hal_getTimeInMs();
  487. self->waitingForResponse = true;
  488. newState = PLL_EXECUTE_RESET_REMOTE_LINK;
  489. llpb_setNewState(self, LL_STATE_BUSY);
  490. }
  491. else { /* illegal message in this state */
  492. newState = PLL_IDLE;
  493. llpb_setNewState(self, LL_STATE_ERROR);
  494. }
  495. break;
  496. case LL_FC_14_SERVICE_NOT_FUNCTIONING:
  497. case LL_FC_15_SERVICE_NOT_IMPLEMENTED:
  498. printf ("PLL - link layer service not functioning/not implemented in secondary station\n");
  499. if (self->sendLinkLayerTestFunction)
  500. self->sendLinkLayerTestFunction = false;
  501. if (primaryState == PLL_EXECUTE_SERVICE_SEND_CONFIRM) {
  502. newState = PLL_LINK_LAYERS_AVAILABLE;
  503. llpb_setNewState(self, LL_STATE_AVAILABLE);
  504. }
  505. break;
  506. default:
  507. printf ("UNEXPECTED SECONDARY LINK LAYER MESSAGE\n");
  508. break;
  509. }
  510. printf("PLL RECV - old state: %i new state %i\n", primaryState, newState);
  511. self->primaryState = newState;
  512. }
  513. static void
  514. llsc_setState(LinkLayerSlaveConnection self, LinkLayerState newState)
  515. {
  516. if (self->state != newState) {
  517. self->state = newState;
  518. if (self->primaryLink->stateChangedHandler)
  519. self->primaryLink->stateChangedHandler(self->primaryLink->stateChangedHandlerParameter, self->address, newState);
  520. }
  521. }
  522. static void
  523. LinkLayerSlaveConnection_HandleMessage(LinkLayerSlaveConnection self, uint8_t fc, bool acd, bool dfc, int address, uint8_t* msg, int userDataStart, int userDataLength)
  524. {
  525. IPrimaryApplicationLayer applicationLayer = self->primaryLink->applicationLayer;
  526. PrimaryLinkLayerState primaryState = self->primaryState;
  527. PrimaryLinkLayerState newState = primaryState;
  528. if (dfc) {
  529. printf ("[SLAVE %i] PLL - DFC = true!\n", self->address);
  530. /* stop sending ASDUs; only send Status of link requests */
  531. self->dontSendMessages = true;
  532. switch (self->primaryState) {
  533. case PLL_EXECUTE_REQUEST_STATUS_OF_LINK:
  534. case PLL_EXECUTE_RESET_REMOTE_LINK:
  535. newState = PLL_EXECUTE_REQUEST_STATUS_OF_LINK;
  536. break;
  537. case PLL_EXECUTE_SERVICE_SEND_CONFIRM:
  538. case PLL_SECONDARY_LINK_LAYER_BUSY:
  539. newState = PLL_SECONDARY_LINK_LAYER_BUSY;
  540. break;
  541. default:
  542. break;
  543. }
  544. llsc_setState(self, LL_STATE_BUSY);
  545. self->primaryState = newState;
  546. return;
  547. }
  548. else {
  549. /* unblock transmission of application layer messages */
  550. self->dontSendMessages = false;
  551. }
  552. if (acd)
  553. self->requestClass1Data = true;
  554. switch (fc) {
  555. case LL_FC_00_ACK:
  556. printf ("[SLAVE %i] PLL - received ACK\n", self->address);
  557. if (primaryState == PLL_EXECUTE_RESET_REMOTE_LINK) {
  558. newState = PLL_LINK_LAYERS_AVAILABLE;
  559. llsc_setState(self, LL_STATE_AVAILABLE);
  560. }
  561. else if (primaryState == PLL_EXECUTE_SERVICE_SEND_CONFIRM) {
  562. if (self->sendLinkLayerTestFunction)
  563. self->sendLinkLayerTestFunction = false;
  564. else
  565. self->hasMessageToSend = false;
  566. llsc_setState(self, LL_STATE_AVAILABLE);
  567. newState = PLL_LINK_LAYERS_AVAILABLE;
  568. }
  569. else if (primaryState == PLL_EXECUTE_SERVICE_REQUEST_RESPOND) {
  570. /* single char ACK is interpreted as RESP NO DATA */
  571. llsc_setState(self, LL_STATE_AVAILABLE);
  572. newState = PLL_LINK_LAYERS_AVAILABLE;
  573. }
  574. self->waitingForResponse = false;
  575. break;
  576. case LL_FC_01_NACK:
  577. printf ("[SLAVE %i] PLL - received NACK\n", self->address);
  578. if (primaryState == PLL_EXECUTE_SERVICE_SEND_CONFIRM) {
  579. llsc_setState(self, LL_STATE_BUSY);
  580. newState = PLL_SECONDARY_LINK_LAYER_BUSY;
  581. }
  582. self->waitingForResponse = false;
  583. break;
  584. case LL_FC_11_STATUS_OF_LINK_OR_ACCESS_DEMAND:
  585. printf ("[SLAVE %i] PLL - received STATUS OF LINK\n", self->address);
  586. if (primaryState == PLL_EXECUTE_REQUEST_STATUS_OF_LINK) {
  587. printf ("[SLAVE %i] PLL - SEND RESET REMOTE LINK\n", self->address);
  588. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_00_RESET_REMOTE_LINK, self->address, true, false, false, false);
  589. self->lastSendTime = Hal_getTimeInMs();
  590. self->waitingForResponse = true;
  591. newState = PLL_EXECUTE_RESET_REMOTE_LINK;
  592. llsc_setState(self, LL_STATE_BUSY);
  593. }
  594. else { /* illegal message */
  595. newState = PLL_IDLE;
  596. llsc_setState(self, LL_STATE_ERROR);
  597. }
  598. break;
  599. case LL_FC_08_RESP_USER_DATA:
  600. printf ("[SLAVE %i] PLL - received USER DATA\n", self->address);
  601. if (primaryState == PLL_EXECUTE_SERVICE_REQUEST_RESPOND) {
  602. if (applicationLayer->UserData)
  603. applicationLayer->UserData(self->primaryLink->applicationLayerParam, address, msg, userDataStart, userDataLength);
  604. self->requestClass1Data = false;
  605. self->requestClass2Data = false;
  606. newState = PLL_LINK_LAYERS_AVAILABLE;
  607. llsc_setState(self, LL_STATE_AVAILABLE);
  608. }
  609. else { /* illegal message */
  610. newState = PLL_IDLE;
  611. llsc_setState(self, LL_STATE_ERROR);
  612. }
  613. self->waitingForResponse = false;
  614. break;
  615. case LL_FC_09_RESP_NACK_NO_DATA:
  616. printf ("[SLAVE %i] PLL - received RESP NO DATA\n", self->address);
  617. if (primaryState == PLL_EXECUTE_SERVICE_REQUEST_RESPOND) {
  618. newState = PLL_LINK_LAYERS_AVAILABLE;
  619. llsc_setState(self, LL_STATE_AVAILABLE);
  620. }
  621. else { /* illegal message */
  622. newState = PLL_IDLE;
  623. llsc_setState(self, LL_STATE_ERROR);
  624. }
  625. self->waitingForResponse = false;
  626. break;
  627. case LL_FC_14_SERVICE_NOT_FUNCTIONING:
  628. case LL_FC_15_SERVICE_NOT_IMPLEMENTED:
  629. printf ("[SLAVE %i] PLL - link layer service not functioning/not implemented in secondary station\n", self->address);
  630. if (primaryState == PLL_EXECUTE_SERVICE_SEND_CONFIRM) {
  631. newState = PLL_LINK_LAYERS_AVAILABLE;
  632. llsc_setState(self, LL_STATE_AVAILABLE);
  633. }
  634. self->waitingForResponse = false;
  635. break;
  636. default:
  637. printf ("[SLAVE %i] UNEXPECTED SECONDARY LINK LAYER MESSAGE\n", self->address);
  638. self->waitingForResponse = false;
  639. break;
  640. }
  641. if (acd) {
  642. if (applicationLayer->AccessDemand)
  643. applicationLayer->AccessDemand(self->primaryLink->applicationLayerParam, address);
  644. }
  645. if (primaryState != newState)
  646. printf("[SLAVE %i] PLL RECV: old state: %i new state: %i\n", self->address, primaryState, newState);
  647. self->primaryState = newState;
  648. }
  649. void
  650. LinkLayerPrimaryUnbalanced_handleMessage(LinkLayerPrimaryUnbalanced self, uint8_t fc, bool acd, bool dfc, int address,
  651. uint8_t* msg, int userDataStart, int userDataLength)
  652. {
  653. LinkLayerSlaveConnection slave = NULL;
  654. if (address == -1)
  655. slave = self->currentSlave;
  656. else
  657. slave = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, address);
  658. if (slave) {
  659. LinkLayerSlaveConnection_HandleMessage(slave, fc, acd, dfc, address, msg, userDataStart, userDataLength);
  660. }
  661. else {
  662. printf ("PLL RECV - response from unknown slave %i\n", address);
  663. }
  664. }
  665. static void
  666. HandleMessageBalancedAndPrimaryUnbalanced(void* parameter, uint8_t* msg, int msgSize)
  667. {
  668. LinkLayer self = (LinkLayer) parameter;
  669. int userDataLength = 0;
  670. int userDataStart = 0;
  671. uint8_t c = 0;
  672. int csStart = 0;
  673. int csIndex = 0;
  674. int address = 0; /* address can be ignored in balanced mode? */
  675. bool isSingleCharAck = false;
  676. if (msg[0] == 0x68) {
  677. if (msg[1] != msg[2]) {
  678. printf ("ERROR: L fields differ!\n");
  679. return;
  680. }
  681. userDataLength = msg[1] - self->linkLayerParameters->addressLength - 1;
  682. userDataStart = 5 + self->linkLayerParameters->addressLength;
  683. csStart = 4;
  684. csIndex = userDataStart + userDataLength;
  685. /* check if message size is reasonable */
  686. if (msgSize != (userDataStart + userDataLength + 2 /* CS + END */)) {
  687. printf ("ERROR: Invalid message length\n");
  688. return;
  689. }
  690. c = msg[4];
  691. if (self->linkLayerParameters->addressLength > 0)
  692. address += msg[5];
  693. if (self->linkLayerParameters->addressLength > 1)
  694. address += msg[6] * 0x100;
  695. } else if (msg[0] == 0x10) {
  696. c = msg[1];
  697. csStart = 1;
  698. csIndex = 2 + self->linkLayerParameters->addressLength;
  699. if (self->linkLayerParameters->addressLength > 0)
  700. address += msg[2];
  701. if (self->linkLayerParameters->addressLength > 1)
  702. address += msg[3] * 0x100;
  703. } else if (msg[0] == 0xe5) {
  704. isSingleCharAck = true;
  705. printf ("Received single char ACK\n");
  706. }
  707. else {
  708. printf("ERROR: Received unexpected message type!\n");
  709. return;
  710. }
  711. if (isSingleCharAck == false) {
  712. /* check checksum */
  713. uint8_t checksum = 0;
  714. int i;
  715. for (i = csStart; i < csIndex; i++)
  716. checksum += msg [i];
  717. if (checksum != msg [csIndex]) {
  718. printf ("ERROR: checksum invalid!\n");
  719. return;
  720. }
  721. /* parse C field bits */
  722. uint8_t fc = c & 0x0f;
  723. bool prm = ((c & 0x40) == 0x40);
  724. if (prm) { /* we are secondary link layer */
  725. bool fcb = ((c & 0x20) == 0x20);
  726. bool fcv = ((c & 0x10) == 0x10);
  727. if (self->llSecBalanced != NULL)
  728. LinkLayerSecondaryBalanced_handleMessage(self->llSecBalanced, fc, false, fcb, fcv, msg, userDataStart, userDataLength);
  729. else
  730. printf ("No secondary link layer available!\n");
  731. if (self->llPriBalanced != NULL) {
  732. LinkLayerPrimaryBalanced_resetIdleTimeout(self->llPriBalanced);
  733. }
  734. } else { /* we are primary link layer */
  735. bool dir = ((c & 0x80) == 0x80); /* DIR - direction for balanced transmission */
  736. bool dfc = ((c & 0x10) == 0x10); /* DFC - Data flow control */
  737. if (self->llPriBalanced != NULL) {
  738. LinkLayerPrimaryBalanced_handleMessage(self->llPriBalanced, fc, dir, dfc, address, msg, userDataStart, userDataLength);
  739. }
  740. else if (self->llPriUnbalanced != NULL) {
  741. bool acd = ((c & 0x20) == 0x20); /* ACD - access demand for class 1 data - for unbalanced transmission */
  742. LinkLayerPrimaryUnbalanced_handleMessage(self->llPriUnbalanced, fc, acd, dfc, address, msg, userDataStart, userDataLength);
  743. }
  744. else
  745. printf ("No primary link layer available!\n");
  746. }
  747. }
  748. else { /* Single byte ACK */
  749. if (self->llPriBalanced != NULL) {
  750. LinkLayerPrimaryBalanced_handleMessage(self->llPriBalanced, LL_FC_00_ACK, false, false, -1, NULL, 0, 0);
  751. }
  752. else if (self->llPriUnbalanced != NULL) {
  753. LinkLayerPrimaryUnbalanced_handleMessage(self->llPriUnbalanced, LL_FC_00_ACK, false, false, -1, NULL, 0, 0);
  754. }
  755. else
  756. printf ("No primary link layer available!\n");
  757. }
  758. }
  759. static void
  760. SendVariableLengthFrame(LinkLayer self, uint8_t fc, int address, bool prm, bool dir, bool acd, bool dfc, Frame frame)
  761. {
  762. uint8_t* buffer = self->buffer;
  763. int addressLength = self->linkLayerParameters->addressLength;
  764. buffer [0] = 0x68; /* START */
  765. buffer [3] = 0x68; /* START */
  766. uint8_t c = fc & 0x0f;
  767. if (prm) c+= 0x40;//0100 0000
  768. if (dir) c+= 0x80;//1000 0000
  769. if (acd) c+= 0x20;//0010 0000
  770. if (dfc) c+= 0x10;//0001 0000
  771. buffer [4] = c;
  772. int bufPos = 5;
  773. if (addressLength > 0) {
  774. buffer [bufPos++] = (uint8_t) (address % 0x100);
  775. if (addressLength > 1)
  776. buffer [bufPos++] = (uint8_t) ((address / 0x100) % 0x100);
  777. }
  778. uint8_t* userData = Frame_getBuffer(frame);
  779. int userDataLength = Frame_getMsgSize(frame);
  780. int l = 1 + addressLength + userDataLength;
  781. if (l > 255)
  782. return;
  783. buffer [1] = (uint8_t) l;
  784. buffer [2] = (uint8_t) l;
  785. int i;
  786. for (i = 0; i < userDataLength; i++)
  787. buffer [bufPos++] = userData[i];
  788. uint8_t checksum = 0;
  789. for (i = 4; i < bufPos; i++)
  790. checksum += buffer[i];
  791. buffer [bufPos++] = checksum;
  792. buffer [bufPos++] = 0x16; /* END */
  793. printf("Send variable frame (fc=%i, size=%i)\n", (int) fc, bufPos);
  794. SerialTransceiverFT12_sendMessage(self->transceiver, buffer, bufPos);
  795. }
  796. static int
  797. LinkLayer_getBroadcastAddress(LinkLayer self)
  798. {
  799. if (self->linkLayerParameters->addressLength == 1) {
  800. return 255;
  801. } else if (self->linkLayerParameters->addressLength == 2) {
  802. return 65535;
  803. }
  804. return 0;
  805. }
  806. static void
  807. LinkLayerSlaveConnection_runStateMachine(LinkLayerSlaveConnection self)
  808. {
  809. uint32_t currentTime = Hal_getTimeInMs();
  810. PrimaryLinkLayerState primaryState = self->primaryState;
  811. PrimaryLinkLayerState newState = primaryState;
  812. switch (primaryState) {
  813. case PLL_TIMEOUT:
  814. if (self->lastSendTime > currentTime) {
  815. /* last sent time not plausible! */
  816. self->lastSendTime = currentTime;
  817. }
  818. if (currentTime > (self->lastSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutLinkState)) {
  819. newState = PLL_IDLE;
  820. }
  821. break;
  822. case PLL_IDLE:
  823. self->originalSendTime = 0;
  824. self->sendLinkLayerTestFunction = false;
  825. printf ("[SLAVE %i] PLL - SEND FC 09 - REQUEST LINK STATUS\n", self->address);
  826. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_09_REQUEST_LINK_STATUS, self->address, true, false, false, false);
  827. self->lastSendTime = currentTime;
  828. self->waitingForResponse = true;
  829. newState = PLL_EXECUTE_REQUEST_STATUS_OF_LINK;
  830. break;
  831. case PLL_EXECUTE_REQUEST_STATUS_OF_LINK:
  832. if (self->waitingForResponse) {
  833. if (self->lastSendTime > currentTime) {
  834. /* last sent time not plausible! */
  835. self->lastSendTime = currentTime;
  836. }
  837. if (currentTime > (self->lastSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutForAck)) {
  838. self->waitingForResponse = false;
  839. self->lastSendTime = currentTime;
  840. newState = PLL_TIMEOUT;
  841. }
  842. }
  843. else {
  844. printf ("[SLAVE %i] PLL - SEND FC 00 - RESET REMOTE LINK\n", self->address);
  845. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_00_RESET_REMOTE_LINK, self->address, true, false, false, false);
  846. self->lastSendTime = currentTime;
  847. self->waitingForResponse = true;
  848. self->nextFcb = true;
  849. newState = PLL_EXECUTE_RESET_REMOTE_LINK;
  850. }
  851. break;
  852. case PLL_EXECUTE_RESET_REMOTE_LINK:
  853. if (self->waitingForResponse) {
  854. if (self->lastSendTime > currentTime) {
  855. /* last sent time not plausible! */
  856. self->lastSendTime = currentTime;
  857. }
  858. if (currentTime > (self->lastSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutForAck)) {
  859. self->waitingForResponse = false;
  860. self->lastSendTime = currentTime;
  861. newState = PLL_TIMEOUT;
  862. llsc_setState(self, LL_STATE_ERROR);
  863. }
  864. }
  865. else {
  866. newState = PLL_LINK_LAYERS_AVAILABLE;
  867. llsc_setState(self, LL_STATE_AVAILABLE);
  868. }
  869. break;
  870. case PLL_LINK_LAYERS_AVAILABLE:
  871. if (self->sendLinkLayerTestFunction) {
  872. printf ("[SLAVE %i] PLL - FC 02 - SEND TEST LINK\n", self->address);
  873. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_02_TEST_FUNCTION_FOR_LINK, self->address, true, false, self->nextFcb, true);
  874. self->nextFcb = !(self->nextFcb);
  875. self->lastSendTime = currentTime;
  876. self->originalSendTime = currentTime;
  877. self->waitingForResponse = true;
  878. newState = PLL_EXECUTE_SERVICE_REQUEST_RESPOND;
  879. }
  880. else if (self->hasMessageToSend) {
  881. printf ("[SLAVE %i] PLL - SEND FC 03 - USER DATA CONFIRMED\n", self->address);
  882. SendVariableLengthFrame(self->primaryLink->linkLayer, LL_FC_03_USER_DATA_CONFIRMED, self->address, true, false, self->nextFcb, true, (Frame) &(self->nextMessage));
  883. self->nextFcb = !(self->nextFcb);
  884. self->lastSendTime = currentTime;
  885. self->originalSendTime = currentTime;
  886. self->waitingForResponse = true;
  887. newState = PLL_EXECUTE_SERVICE_SEND_CONFIRM;
  888. }
  889. else if (self->requestClass1Data || self->requestClass2Data) {
  890. if (self->requestClass1Data) {
  891. printf ("[SLAVE %i] PLL - SEND FC 10 - REQ UD 1\n", self->address);
  892. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_10_REQUEST_USER_DATA_CLASS_1, self->address, true, false, self->nextFcb, true);
  893. self->requestClass1Data = false;
  894. }
  895. else {
  896. printf ("[SLAVE %i] PLL - SEND FC 11 - REQ UD 2\n", self->address);
  897. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_11_REQUEST_USER_DATA_CLASS_2, self->address, true, false, self->nextFcb, true);
  898. self->requestClass2Data = false;
  899. }
  900. self->nextFcb = !(self->nextFcb);
  901. self->lastSendTime = currentTime;
  902. self->originalSendTime = currentTime;
  903. self->waitingForResponse = true;
  904. newState = PLL_EXECUTE_SERVICE_REQUEST_RESPOND;
  905. }
  906. break;
  907. case PLL_EXECUTE_SERVICE_SEND_CONFIRM:
  908. if (self->lastSendTime > currentTime) {
  909. /* last sent time not plausible! */
  910. self->lastSendTime = currentTime;
  911. }
  912. if (currentTime > (self->lastSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutForAck)) {
  913. if (currentTime > (self->originalSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutRepeat)) {
  914. printf ("[SLAVE %i] TIMEOUT: ASDU not confirmed after repeated transmission\n", self->address);
  915. self->waitingForResponse = false;
  916. self->lastSendTime = currentTime;
  917. newState = PLL_TIMEOUT;
  918. llsc_setState(self, LL_STATE_ERROR);
  919. }
  920. else {
  921. printf ("[SLAVE %i] TIMEOUT: ASDU not confirmed\n", self->address);
  922. if (self->sendLinkLayerTestFunction) {
  923. printf ("[SLAVE %i] PLL - SEND FC 02 - RESET REMOTE LINK [REPEAT]\n", self->address);
  924. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_02_TEST_FUNCTION_FOR_LINK, self->address, true, false, !(self->nextFcb), true);
  925. }
  926. else {
  927. printf ("[SLAVE %i] PLL - SEND FC 03 - USER DATA CONFIRMED [REPEAT]\n", self->address);
  928. SendVariableLengthFrame(self->primaryLink->linkLayer, LL_FC_03_USER_DATA_CONFIRMED, self->address, true, false, !(self->nextFcb), true, (Frame) &(self->nextMessage));
  929. }
  930. self->lastSendTime = currentTime;
  931. }
  932. }
  933. break;
  934. case PLL_EXECUTE_SERVICE_REQUEST_RESPOND:
  935. if (self->lastSendTime > currentTime) {
  936. /* last sent time not plausible! */
  937. self->lastSendTime = currentTime;
  938. }
  939. if (currentTime > (self->lastSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutForAck)) {
  940. if (currentTime > (self->originalSendTime + self->primaryLink->linkLayer->linkLayerParameters->timeoutRepeat)) {
  941. printf ("[SLAVE %i] TIMEOUT: ASDU not confirmed after repeated transmission\n", self->address);
  942. newState = PLL_IDLE;
  943. self->requestClass1Data = false;
  944. self->requestClass2Data = false;
  945. llsc_setState(self, LL_STATE_ERROR);
  946. }
  947. else {
  948. printf ("[SLAVE %i] TIMEOUT: ASDU not confirmed\n", self->address);
  949. if (self->requestClass1Data) {
  950. printf ("[SLAVE %i] PLL - SEND FC 10 - REQ UD 1 [REPEAT]\n", self->address);
  951. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_10_REQUEST_USER_DATA_CLASS_1, self->address, true, false, !(self->nextFcb), true);
  952. }
  953. else {
  954. printf ("[SLAVE %i] PLL - SEND FC 11 - REQ UD 2 [REPEAT]\n", self->address);
  955. SendFixedFrame(self->primaryLink->linkLayer, LL_FC_11_REQUEST_USER_DATA_CLASS_2, self->address, true, false, !(self->nextFcb), true);
  956. }
  957. self->lastSendTime = currentTime;
  958. }
  959. }
  960. break;
  961. case PLL_SECONDARY_LINK_LAYER_BUSY:
  962. break;
  963. }
  964. if (primaryState != newState)
  965. printf("[SLAVE %i] PLL: old state: %i new state: %i\n", self->address, primaryState, newState);
  966. self->primaryState = newState;
  967. }
  968. void
  969. LinkLayerPrimaryUnbalanced_runStateMachine(LinkLayerPrimaryUnbalanced self)
  970. {
  971. if (self->hasNextBroadcastToSend) {
  972. /* send pending broadcast message */
  973. SendVariableLengthFrame(self->linkLayer, LL_FC_04_USER_DATA_NO_REPLY, LinkLayer_getBroadcastAddress(self->linkLayer), true, false, false, false, (Frame) &(self->nextBroadcastMessage));
  974. self->hasNextBroadcastToSend = false;
  975. }
  976. /* run all the link layer state machines for the registered slaves */
  977. if (LinkedList_size(self->slaveConnections) > 0) {
  978. if (self->currentSlave != NULL) {
  979. if (self->currentSlave->waitingForResponse == false)
  980. self->currentSlave = NULL;
  981. }
  982. if (self->currentSlave == NULL) {
  983. /* schedule next slave connection */
  984. self->currentSlave = (LinkLayerSlaveConnection) LinkedList_getData(LinkedList_get(self->slaveConnections, self->currentSlaveIndex));
  985. self->currentSlaveIndex = (self->currentSlaveIndex + 1) % LinkedList_size(self->slaveConnections);
  986. }
  987. if (self->currentSlave)
  988. LinkLayerSlaveConnection_runStateMachine(self->currentSlave);
  989. }
  990. }
  991. void
  992. LinkLayerPrimaryBalanced_runStateMachine(LinkLayerPrimaryBalanced self)
  993. {
  994. uint64_t currentTime = Hal_getTimeInMs();
  995. PrimaryLinkLayerState primaryState = self->primaryState;
  996. PrimaryLinkLayerState newState = primaryState;
  997. switch (primaryState) {
  998. case PLL_IDLE:
  999. self->originalSendTime = 0;
  1000. self->sendLinkLayerTestFunction = false;
  1001. SendFixedFrame(self->linkLayer, LL_FC_09_REQUEST_LINK_STATUS, self->otherStationAddress, true, self->linkLayer->dir, false, false);
  1002. self->lastSendTime = currentTime;
  1003. self->waitingForResponse = true;
  1004. newState = PLL_EXECUTE_REQUEST_STATUS_OF_LINK;
  1005. break;
  1006. case PLL_EXECUTE_REQUEST_STATUS_OF_LINK:
  1007. if (self->waitingForResponse) {
  1008. if (self->lastSendTime > currentTime) {
  1009. /* last sent time not plausible! */
  1010. self->lastSendTime = currentTime;
  1011. }
  1012. if (currentTime > (self->lastSendTime + self->linkLayer->linkLayerParameters->timeoutForAck)) {
  1013. newState = PLL_IDLE;
  1014. }
  1015. }
  1016. else {
  1017. printf ("PLL - SEND RESET REMOTE LINK\n");
  1018. SendFixedFrame(self->linkLayer, LL_FC_00_RESET_REMOTE_LINK, self->otherStationAddress, true, self->linkLayer->dir, false, false);
  1019. self->lastSendTime = currentTime;
  1020. self->waitingForResponse = true;
  1021. newState = PLL_EXECUTE_RESET_REMOTE_LINK;
  1022. }
  1023. break;
  1024. case PLL_EXECUTE_RESET_REMOTE_LINK:
  1025. if (self->waitingForResponse) {
  1026. if (self->lastSendTime > currentTime) {
  1027. /* last sent time not plausible! */
  1028. self->lastSendTime = currentTime;
  1029. }
  1030. if (currentTime > (self->lastSendTime + self->linkLayer->linkLayerParameters->timeoutForAck)) {
  1031. self->waitingForResponse = false;
  1032. newState = PLL_IDLE;
  1033. llpb_setNewState(self, LL_STATE_ERROR);
  1034. }
  1035. }
  1036. else {
  1037. newState = PLL_LINK_LAYERS_AVAILABLE;
  1038. llpb_setNewState(self, LL_STATE_AVAILABLE);
  1039. }
  1040. break;
  1041. case PLL_LINK_LAYERS_AVAILABLE:
  1042. if (self->lastReceivedMsg > currentTime) {
  1043. /* last received message not plausible */
  1044. self->lastReceivedMsg = currentTime;
  1045. }
  1046. if ((currentTime - self->lastReceivedMsg) > (unsigned int) self->idleTimeout) {
  1047. printf ("PLL - Idle timeout detected. Send link layer test function\n");
  1048. self->sendLinkLayerTestFunction = true;
  1049. }
  1050. if (self->sendLinkLayerTestFunction) {
  1051. printf ("PLL - SEND TEST LINK\n");
  1052. SendFixedFrame(self->linkLayer, LL_FC_02_TEST_FUNCTION_FOR_LINK, self->otherStationAddress, true, self->linkLayer->dir, self->nextFcb, true);
  1053. self->nextFcb = !(self->nextFcb);
  1054. self->lastSendTime = currentTime;
  1055. self->originalSendTime = self->lastSendTime;
  1056. newState = PLL_EXECUTE_SERVICE_SEND_CONFIRM;
  1057. }
  1058. else {
  1059. /* provide a buffer where the application layer can encode the user data */
  1060. Frame bufferFrame = BufferFrame_initialize(&(self->lastSendAsdu), self->linkLayer->userDataBuffer, 0);
  1061. Frame asdu = self->applicationLayer->GetUserData(self->applicationLayerParam, bufferFrame);
  1062. if (asdu) {
  1063. printf ("PLL: SEND USER DATA CONFIRMED\n");
  1064. SendVariableLengthFrame(self->linkLayer, LL_FC_03_USER_DATA_CONFIRMED, self->otherStationAddress, true, self->linkLayer->dir, self->nextFcb, true, asdu);
  1065. self->nextFcb = !(self->nextFcb);
  1066. self->lastSendTime = currentTime;
  1067. self->originalSendTime = self->lastSendTime;
  1068. self->waitingForResponse = true;
  1069. newState = PLL_EXECUTE_SERVICE_SEND_CONFIRM;
  1070. }
  1071. }
  1072. break;
  1073. case PLL_EXECUTE_SERVICE_SEND_CONFIRM:
  1074. if (self->lastSendTime > currentTime) {
  1075. /* last sent time not plausible! */
  1076. self->lastSendTime = currentTime;
  1077. }
  1078. if (currentTime > (self->lastSendTime + self->linkLayer->linkLayerParameters->timeoutForAck)) {
  1079. if (currentTime > (self->originalSendTime + self->linkLayer->linkLayerParameters->timeoutRepeat)) {
  1080. printf ("TIMEOUT: ASDU not confirmed after repeated transmission\n");
  1081. newState = PLL_IDLE;
  1082. llpb_setNewState(self, LL_STATE_ERROR);
  1083. }
  1084. else {
  1085. printf ("TIMEOUT: ASDU not confirmed\n");
  1086. if (self->sendLinkLayerTestFunction) {
  1087. printf ("PLL - repeat send test function\n");
  1088. SendFixedFrame(self->linkLayer, LL_FC_02_TEST_FUNCTION_FOR_LINK, self->otherStationAddress, true, self->linkLayer->dir, !(self->nextFcb), true);
  1089. }
  1090. else {
  1091. printf ("PLL - repeat last ASDU\n");
  1092. SendVariableLengthFrame(self->linkLayer, LL_FC_03_USER_DATA_CONFIRMED, self->otherStationAddress, true, self->linkLayer->dir, !(self->nextFcb), true, (Frame) &(self->lastSendAsdu));
  1093. }
  1094. self->lastSendTime = currentTime;
  1095. }
  1096. }
  1097. break;
  1098. case PLL_SECONDARY_LINK_LAYER_BUSY:
  1099. break;
  1100. default:
  1101. break;
  1102. }
  1103. if (primaryState != newState)
  1104. printf ("PLL - old state: %i new state: %i\n", primaryState, newState);
  1105. self->primaryState = newState;
  1106. }
  1107. void
  1108. LinkLayerBalanced_setAddress(LinkLayerBalanced self, int address)
  1109. {
  1110. self->linkLayer->address = address;
  1111. }
  1112. void
  1113. LinkLayerSecondaryUnbalanced_setAddress(LinkLayerSecondaryUnbalanced self, int address)
  1114. {
  1115. self->_linkLayer.address = address;
  1116. }
  1117. void
  1118. LinkLayerBalanced_run(LinkLayerBalanced self)
  1119. {
  1120. LinkLayer ll = self->linkLayer;
  1121. SerialTransceiverFT12_readNextMessage(ll->transceiver, ll->buffer, HandleMessageBalancedAndPrimaryUnbalanced, (void*) ll);
  1122. LinkLayerPrimaryBalanced_runStateMachine(&(self->primaryLinkLayer));
  1123. }
  1124. void
  1125. LinkLayerPrimaryUnbalanced_run(LinkLayerPrimaryUnbalanced self)
  1126. {
  1127. LinkLayer ll = self->linkLayer;
  1128. SerialTransceiverFT12_readNextMessage(ll->transceiver, ll->buffer, HandleMessageBalancedAndPrimaryUnbalanced, (void*) ll);
  1129. LinkLayerPrimaryUnbalanced_runStateMachine(self);
  1130. }
  1131. static bool
  1132. llsc_isMessageWaitingToSend(LinkLayerSlaveConnection self)
  1133. {
  1134. if ((self->requestClass1Data) || (self->requestClass2Data) || (self->hasMessageToSend))
  1135. return true;
  1136. else
  1137. return false;
  1138. }
  1139. bool
  1140. LinkLayerPrimaryUnbalanced_isChannelAvailable(LinkLayerPrimaryUnbalanced self, int slaveAddress)
  1141. {
  1142. LinkLayerSlaveConnection slave = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, slaveAddress);
  1143. if (slave)
  1144. return !(llsc_isMessageWaitingToSend(slave));
  1145. return false;
  1146. }
  1147. void
  1148. LinkLayerBalanced_setOtherStationAddress(LinkLayerBalanced self, int address)
  1149. {
  1150. self->primaryLinkLayer.otherStationAddress = address;
  1151. }
  1152. bool
  1153. LinkLayerPrimaryUnbalanced_sendNoReply(LinkLayerPrimaryUnbalanced self, int slaveAddress, BufferFrame message)
  1154. {
  1155. if (slaveAddress == LinkLayer_getBroadcastAddress(self->linkLayer)) {
  1156. if (self->hasNextBroadcastToSend)
  1157. return false;
  1158. else {
  1159. self->nextBroadcastMessage.msgSize = message->msgSize;
  1160. self->nextBroadcastMessage.startSize = message->startSize;
  1161. memcpy(self->nextBroadcastMessage.buffer, message->buffer, message->msgSize);
  1162. self->hasNextBroadcastToSend = true;
  1163. return true;
  1164. }
  1165. }
  1166. else {
  1167. LinkLayerSlaveConnection slave = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, slaveAddress);
  1168. if (slave) {
  1169. if (slave->hasMessageToSend == false) {
  1170. slave->nextMessage.msgSize = message->msgSize;
  1171. slave->nextMessage.startSize = message->startSize;
  1172. memcpy(slave->nextMessage.buffer, message->buffer, message->msgSize);
  1173. slave->hasMessageToSend = true;
  1174. return true;
  1175. }
  1176. }
  1177. }
  1178. return false;
  1179. }
  1180. bool
  1181. LinkLayerPrimaryUnbalanced_sendConfirmed(LinkLayerPrimaryUnbalanced self, int slaveAddress, BufferFrame message)
  1182. {
  1183. LinkLayerSlaveConnection slave = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, slaveAddress);
  1184. if (slave) {
  1185. if (slave->hasMessageToSend == false) {
  1186. slave->nextMessage.msgSize = message->msgSize;
  1187. slave->nextMessage.startSize = message->startSize;
  1188. memcpy(slave->nextMessage.buffer, message->buffer, message->msgSize);
  1189. slave->hasMessageToSend = true;
  1190. return true;
  1191. }
  1192. }
  1193. return false;
  1194. }
  1195. void
  1196. LinkLayerBalanced_destroy(LinkLayerBalanced self)
  1197. {
  1198. if (self) {
  1199. GLOBAL_FREEMEM(self);
  1200. }
  1201. }
  1202. void
  1203. LinkLayerPrimaryUnbalanced_destroy(LinkLayerPrimaryUnbalanced self)
  1204. {
  1205. if (self) {
  1206. if (self->slaveConnections)
  1207. LinkedList_destroy(self->slaveConnections);
  1208. GLOBAL_FREEMEM(self);
  1209. }
  1210. }
  1211. bool
  1212. LinkLayerPrimaryUnbalanced_requestClass1Data(LinkLayerPrimaryUnbalanced self, int slaveAddress)
  1213. {
  1214. LinkLayerSlaveConnection slave = LinkLayerPrimaryUnbalanced_getSlaveConnection(self, slaveAddress);
  1215. if (slave) {
  1216. if (llsc_isMessageWaitingToSend(slave) == false) {
  1217. slave->requestClass1Data = true;
  1218. return true;
  1219. }
  1220. }
  1221. return false;
  1222. }
  1223. ///////////////////////////////////////////////////////////////////////////////////////
  1224. void
  1225. LinkLayerSecondaryUnbalanced_setIdleTimeout(LinkLayerSecondaryUnbalanced self, int timeoutInMs)
  1226. {
  1227. self->idleTimeout = timeoutInMs;
  1228. }
  1229. void
  1230. LinkLayerSecondaryUnbalanced_destroy(LL_Sec_Unb self)
  1231. {
  1232. if (self != NULL)
  1233. GLOBAL_FREEMEM(self);
  1234. }
  1235. static bool
  1236. checkFCB(LL_Sec_Unb self, bool fcb)
  1237. {
  1238. if (fcb != self->expectedFcb)
  1239. return false;
  1240. else {
  1241. self->expectedFcb = !(self->expectedFcb);
  1242. return true;
  1243. }
  1244. }
  1245. static void
  1246. llsu_setState(LL_Sec_Unb self, LinkLayerState newState)
  1247. {
  1248. if (self->state != newState) {
  1249. self->state = newState;
  1250. if (self->stateChangedHandler)
  1251. self->stateChangedHandler(self->stateChangedHandlerParameter, -1, newState);
  1252. }
  1253. }
  1254. static void
  1255. LinkLayerSecondaryUnbalanced_handleMessage(LL_Sec_Unb self,
  1256. uint8_t fc,
  1257. bool isBroadcast,
  1258. bool fcb,
  1259. bool fcv,
  1260. uint8_t* msg, int userDataStart, int userDataLength)
  1261. {
  1262. if (fcv) {
  1263. if (checkFCB(self, fcb) == false) {
  1264. printf("SLL - FCB check failed\n");
  1265. llsu_setState(self, LL_STATE_ERROR);
  1266. return;
  1267. }
  1268. }
  1269. llsu_setState(self, LL_STATE_AVAILABLE);
  1270. switch (fc) {
  1271. case LL_FC_09_REQUEST_LINK_STATUS:
  1272. printf("SLL - REQUEST LINK STATUS\n");
  1273. {
  1274. bool accessDemand = self->applicationLayer->IsClass1DataAvailable(self->appLayerParam);
  1275. SendFixedFrame(self->linkLayer, LL_FC_11_STATUS_OF_LINK_OR_ACCESS_DEMAND, self->linkLayer->address, false, false, accessDemand, false);
  1276. }
  1277. break;
  1278. case LL_FC_00_RESET_REMOTE_LINK:
  1279. printf("SLL - RESET REMOTE LINK\n");
  1280. {
  1281. self->expectedFcb = true;
  1282. if (self->linkLayerParameters->useSingleCharACK)
  1283. SendSingleCharCharacter(self->linkLayer);
  1284. else
  1285. SendFixedFrame(self->linkLayer, LL_FC_00_ACK, self->linkLayer->address, false, false, false, false);
  1286. self->applicationLayer->ResetCUReceived(self->appLayerParam, false);
  1287. }
  1288. break;
  1289. case LL_FC_07_RESET_FCB:
  1290. printf("SLL - RESET FCB\n");
  1291. {
  1292. self->expectedFcb = true;
  1293. if (self->linkLayerParameters->useSingleCharACK)
  1294. SendSingleCharCharacter(self->linkLayer);
  1295. else
  1296. SendFixedFrame(self->linkLayer, LL_FC_00_ACK, self->linkLayer->address, false, false, false, false);
  1297. self->applicationLayer->ResetCUReceived(self->appLayerParam, true);
  1298. }
  1299. break;
  1300. case LL_FC_11_REQUEST_USER_DATA_CLASS_2:
  1301. printf("SLL - REQUEST USER DATA CLASS 2\n");
  1302. {
  1303. /* provide a buffer where the application layer can encode the user data */
  1304. struct sBufferFrame _bufferFrame;
  1305. Frame bufferFrame = BufferFrame_initialize(&_bufferFrame, self->_linkLayer.userDataBuffer, 0);
  1306. Frame asdu = self->applicationLayer->GetClass2Data(self->appLayerParam, bufferFrame);
  1307. bool accessDemand = self->applicationLayer->IsClass1DataAvailable(self->appLayerParam);
  1308. if (asdu != NULL) {
  1309. SendVariableLengthFrame(self->linkLayer, LL_FC_08_RESP_USER_DATA, self->linkLayer->address, false, false, accessDemand, false, asdu);
  1310. /* release frame buffer if required */
  1311. if (asdu != bufferFrame)
  1312. Frame_destroy(asdu);
  1313. }
  1314. else {
  1315. if (self->linkLayerParameters->useSingleCharACK && !accessDemand)
  1316. SendSingleCharCharacter(self->linkLayer);
  1317. else
  1318. SendFixedFrame(self->linkLayer, LL_FC_09_RESP_NACK_NO_DATA, self->linkLayer->address, false, false, accessDemand, false);
  1319. }
  1320. }
  1321. break;
  1322. case LL_FC_10_REQUEST_USER_DATA_CLASS_1:
  1323. printf("SLL - REQUEST USER DATA CLASS 1\n");
  1324. {
  1325. /* provide a buffer where the application layer can encode the user data */
  1326. struct sBufferFrame _bufferFrame;
  1327. Frame bufferFrame = BufferFrame_initialize(&_bufferFrame, self->_linkLayer.userDataBuffer, 0);
  1328. Frame asdu = self->applicationLayer->GetClass1Data(self->appLayerParam, bufferFrame);
  1329. bool accessDemand = self->applicationLayer->IsClass1DataAvailable(self->appLayerParam);
  1330. if (asdu != NULL) {
  1331. SendVariableLengthFrame(self->linkLayer, LL_FC_08_RESP_USER_DATA, self->linkLayer->address, false, false, accessDemand, false, asdu);
  1332. /* release frame buffer if required */
  1333. if (asdu != bufferFrame)
  1334. Frame_destroy(asdu);
  1335. }
  1336. else {
  1337. if (self->linkLayerParameters->useSingleCharACK && !accessDemand)
  1338. SendSingleCharCharacter(self->linkLayer);
  1339. else
  1340. SendFixedFrame(self->linkLayer, LL_FC_09_RESP_NACK_NO_DATA, self->linkLayer->address, false, false, accessDemand, false);
  1341. }
  1342. }
  1343. break;
  1344. case LL_FC_03_USER_DATA_CONFIRMED:
  1345. printf ("SLL - USER DATA CONFIRMED\n");
  1346. if (userDataLength > 0) {
  1347. if (self->applicationLayer->HandleReceivedData(self->appLayerParam, msg, isBroadcast, userDataStart, userDataLength)) {
  1348. bool accessDemand = self->applicationLayer->IsClass1DataAvailable(self->appLayerParam);
  1349. if (self->linkLayerParameters->useSingleCharACK && !accessDemand)
  1350. SendSingleCharCharacter(self->linkLayer);
  1351. else
  1352. SendFixedFrame(self->linkLayer, LL_FC_00_ACK, self->linkLayer->address, false, false, accessDemand, false);
  1353. }
  1354. }
  1355. break;
  1356. case LL_FC_04_USER_DATA_NO_REPLY:
  1357. printf ("SLL - USER DATA NO REPLY\n");
  1358. if (userDataLength > 0) {
  1359. self->applicationLayer->HandleReceivedData(self->appLayerParam, msg, isBroadcast, userDataStart, userDataLength);
  1360. }
  1361. break;
  1362. default:
  1363. printf ("SLL - UNEXPECTED LINK LAYER MESSAGE\n");
  1364. SendFixedFrame(self->linkLayer, LL_FC_15_SERVICE_NOT_IMPLEMENTED, self->linkLayer->address, false, false, false, false);
  1365. break;
  1366. }
  1367. }
  1368. LinkLayerSecondaryUnbalanced
  1369. LinkLayerSecondaryUnbalanced_create(
  1370. int linkLayerAddress,
  1371. SerialTransceiverFT12 transceiver,
  1372. LinkLayerParameters linkLayerParameters,
  1373. ISecondaryApplicationLayer applicationLayer,
  1374. void* applicationLayerParameter)
  1375. {
  1376. LL_Sec_Unb self = (LL_Sec_Unb) GLOBAL_MALLOC(sizeof(struct sLinkLayerSecondaryUnbalanced));
  1377. if (self != NULL) {
  1378. self->expectedFcb = true;
  1379. self->applicationLayer = applicationLayer;
  1380. self->appLayerParam = applicationLayerParameter;
  1381. self->linkLayerParameters = linkLayerParameters;
  1382. self->linkLayer = &(self->_linkLayer);
  1383. self->state = LL_STATE_IDLE;
  1384. self->idleTimeout = 500;
  1385. self->stateChangedHandler = NULL;
  1386. self->lastReceivedMsg = 0;
  1387. LinkLayer_init(self->linkLayer, linkLayerAddress, transceiver, self->linkLayerParameters);
  1388. self->linkLayer->llSecUnbalanced = self;
  1389. }
  1390. return self;
  1391. }
  1392. static void
  1393. ParserHeaderSecondaryUnbalanced(void* parameter, uint8_t* msg, int msgSize)
  1394. {
  1395. LL_Sec_Unb self = (LL_Sec_Unb) parameter;
  1396. self->lastReceivedMsg = Hal_getTimeInMs();
  1397. int userDataLength = 0;
  1398. int userDataStart = 0;
  1399. uint8_t c;
  1400. int csStart;
  1401. int csIndex;
  1402. int address = 0;
  1403. int addressLength = self->linkLayer->linkLayerParameters->addressLength;
  1404. if (msg [0] == 0x68) {
  1405. if (msg [1] != msg [2]) {
  1406. printf("ERROR: L fields differ!\n");
  1407. llsu_setState(self, LL_STATE_ERROR);
  1408. return;
  1409. }
  1410. userDataLength = (int)msg [1] - addressLength - 1;
  1411. userDataStart = 5 +addressLength;
  1412. csStart = 4;
  1413. csIndex = userDataStart + userDataLength;
  1414. /* check if message size is reasonable */
  1415. if (msgSize != (userDataStart + userDataLength + 2 /* CS + END */)) {
  1416. printf("ERROR: Invalid message length\n");
  1417. llsu_setState(self, LL_STATE_ERROR);
  1418. return;
  1419. }
  1420. c = msg [4];
  1421. } else if (msg [0] == 0x10) {
  1422. c = msg [1];
  1423. csStart = 1;
  1424. csIndex = 2 + addressLength;
  1425. } else {
  1426. printf("ERROR: Received unexpected message type in unbalanced slave mode!\n");
  1427. llsu_setState(self, LL_STATE_ERROR);
  1428. return;
  1429. }
  1430. bool isBroadcast = false;
  1431. /* check address */
  1432. if (addressLength > 0) {
  1433. address = msg [csStart + 1];
  1434. if (addressLength > 1) {
  1435. address = msg [csStart + 2] * 0x100;
  1436. if (address == 65535)
  1437. isBroadcast = true;
  1438. }
  1439. else {
  1440. if (address == 255)
  1441. isBroadcast = true;
  1442. }
  1443. }
  1444. int fc = c & 0x0f;
  1445. if (isBroadcast) {
  1446. if (fc != LL_FC_04_USER_DATA_NO_REPLY) {
  1447. printf("ERROR: Invalid function code for broadcast message!\n");
  1448. llsu_setState(self, LL_STATE_ERROR);
  1449. return;
  1450. }
  1451. } else {
  1452. if (address != self->linkLayer->address) {
  1453. printf ("INFO: unknown link layer address -> ignore message\n");
  1454. return;
  1455. }
  1456. }
  1457. /* check checksum */
  1458. uint8_t checksum = 0;
  1459. for (int i = csStart; i < csIndex; i++)
  1460. checksum += msg [i];
  1461. if (checksum != msg [csIndex]) {
  1462. printf("ERROR: checksum invalid!\n");
  1463. llsu_setState(self, LL_STATE_ERROR);
  1464. return;
  1465. }
  1466. /* parse C field bits */
  1467. bool prm = ((c & 0x40) == 0x40);
  1468. if (prm == false) {
  1469. printf("ERROR: Received secondary message in unbalanced slave mode!\n");
  1470. llsu_setState(self, LL_STATE_ERROR);
  1471. return;
  1472. }
  1473. bool fcb = ((c & 0x20) == 0x20);
  1474. bool fcv = ((c & 0x10) == 0x10);
  1475. LinkLayerSecondaryUnbalanced_handleMessage(self, fc, isBroadcast, fcb, fcv, msg, userDataStart, userDataLength);
  1476. }
  1477. void
  1478. LinkLayerSecondaryUnbalanced_run(LinkLayerSecondaryUnbalanced self)
  1479. {
  1480. LinkLayer ll = self->linkLayer;
  1481. SerialTransceiverFT12_readNextMessage(ll->transceiver, ll->buffer, ParserHeaderSecondaryUnbalanced, self);
  1482. if (self->state != LL_STATE_IDLE) {
  1483. if ((Hal_getTimeInMs() - self->lastReceivedMsg) > (unsigned int) self->idleTimeout)
  1484. llsu_setState(self, LL_STATE_IDLE);
  1485. }
  1486. }