en50221_session.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. en50221 encoder An implementation for libdvb
  3. an implementation for the en50221 transport layer
  4. Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
  5. Copyright (C) 2005 Julian Scheel (julian at jusst dot de)
  6. Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
  7. This library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as
  9. published by the Free Software Foundation; either version 2.1 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <sys/ioctl.h>
  24. #include <time.h>
  25. #include <libdvbmisc/dvbmisc.h>
  26. #include <sys/uio.h>
  27. #include <pthread.h>
  28. #include "en50221_transport.h"
  29. #include "en50221_session.h"
  30. #include "en50221_errno.h"
  31. #include "asn_1.h"
  32. // these are the possible session statuses
  33. #define S_STATUS_OPEN 0x00 // session is opened
  34. #define S_STATUS_CLOSE_NO_RES 0xF0 // could not open session, no proper resource available
  35. #define S_STATUS_CLOSE_RES_UNAVAILABLE 0xF1 // could not open session, resource unavailable
  36. #define S_STATUS_CLOSE_RES_LOW_VERSION 0xF2 // could not open session, resource version too low
  37. #define S_STATUS_CLOSE_RES_BUSY 0xF3 // could not open session, resource is busy
  38. #define ST_OPEN_SESSION_REQ 0x91 // h<--m
  39. #define ST_OPEN_SESSION_RES 0x92 // h-->m
  40. #define ST_CREATE_SESSION 0x93 // h-->m
  41. #define ST_CREATE_SESSION_RES 0x94 // h<--m
  42. #define ST_CLOSE_SESSION_REQ 0x95 // h<->m
  43. #define ST_CLOSE_SESSION_RES 0x96 // h<->m
  44. #define ST_SESSION_NUMBER 0x90 // h<->m
  45. #define S_STATE_IDLE 0x01 // this session is not in use
  46. #define S_STATE_ACTIVE 0x02 // this session is in use
  47. #define S_STATE_IN_CREATION 0x04 // this session waits for a ST_CREATE_SESSION_RES to become active
  48. #define S_STATE_IN_DELETION 0x08 // this session waits for ST_CLOSE_SESSION_RES to become idle again
  49. // for each session we store its identifier, the resource-id
  50. // it is linked to and the callback of the specific resource
  51. struct en50221_session {
  52. uint8_t state;
  53. uint32_t resource_id;
  54. uint8_t slot_id;
  55. uint8_t connection_id;
  56. en50221_sl_resource_callback callback;
  57. void *callback_arg;
  58. pthread_mutex_t session_lock;
  59. };
  60. struct en50221_session_layer {
  61. uint32_t max_sessions;
  62. struct en50221_transport_layer *tl;
  63. en50221_sl_lookup_callback lookup;
  64. void *lookup_arg;
  65. en50221_sl_session_callback session;
  66. void *session_arg;
  67. pthread_mutex_t global_lock;
  68. pthread_mutex_t setcallback_lock;
  69. int error;
  70. struct en50221_session *sessions;
  71. };
  72. static void en50221_sl_transport_callback(void *arg, int reason,
  73. uint8_t * data,
  74. uint32_t data_length,
  75. uint8_t slot_id,
  76. uint8_t connection_id);
  77. static int en50221_sl_alloc_new_session(struct en50221_session_layer *sl,
  78. uint32_t resource_id,
  79. uint8_t slot_id,
  80. uint8_t connection_id,
  81. en50221_sl_resource_callback
  82. callback, void *arg);
  83. struct en50221_session_layer *en50221_sl_create(struct en50221_transport_layer *tl,
  84. uint32_t max_sessions)
  85. {
  86. struct en50221_session_layer *sl = NULL;
  87. uint32_t i;
  88. // setup structure
  89. sl = (struct en50221_session_layer *)
  90. malloc(sizeof(struct en50221_session_layer));
  91. if (sl == NULL)
  92. goto error_exit;
  93. sl->max_sessions = max_sessions;
  94. sl->lookup = NULL;
  95. sl->session = NULL;
  96. sl->tl = tl;
  97. sl->error = 0;
  98. // init the mutex
  99. pthread_mutex_init(&sl->global_lock, NULL);
  100. pthread_mutex_init(&sl->setcallback_lock, NULL);
  101. // create the slots
  102. sl->sessions = malloc(sizeof(struct en50221_session) * max_sessions);
  103. if (sl->sessions == NULL)
  104. goto error_exit;
  105. // set them up
  106. for (i = 0; i < max_sessions; i++) {
  107. sl->sessions[i].state = S_STATE_IDLE;
  108. sl->sessions[i].callback = NULL;
  109. pthread_mutex_init(&sl->sessions[i].session_lock, NULL);
  110. }
  111. // register ourselves with the transport layer
  112. en50221_tl_register_callback(tl, en50221_sl_transport_callback, sl);
  113. return sl;
  114. error_exit:
  115. en50221_sl_destroy(sl);
  116. return NULL;
  117. }
  118. void en50221_sl_destroy(struct en50221_session_layer *sl)
  119. {
  120. uint32_t i;
  121. if (sl) {
  122. if (sl->sessions) {
  123. for (i = 0; i < sl->max_sessions; i++) {
  124. pthread_mutex_destroy(&sl->sessions[i].session_lock);
  125. }
  126. free(sl->sessions);
  127. }
  128. pthread_mutex_destroy(&sl->setcallback_lock);
  129. pthread_mutex_destroy(&sl->global_lock);
  130. free(sl);
  131. }
  132. }
  133. int en50221_sl_get_error(struct en50221_session_layer *sl)
  134. {
  135. return sl->error;
  136. }
  137. void en50221_sl_register_lookup_callback(struct en50221_session_layer *sl,
  138. en50221_sl_lookup_callback
  139. callback, void *arg)
  140. {
  141. pthread_mutex_lock(&sl->setcallback_lock);
  142. sl->lookup = callback;
  143. sl->lookup_arg = arg;
  144. pthread_mutex_unlock(&sl->setcallback_lock);
  145. }
  146. void en50221_sl_register_session_callback(struct en50221_session_layer *sl,
  147. en50221_sl_session_callback
  148. callback, void *arg)
  149. {
  150. pthread_mutex_lock(&sl->setcallback_lock);
  151. sl->session = callback;
  152. sl->session_arg = arg;
  153. pthread_mutex_unlock(&sl->setcallback_lock);
  154. }
  155. int en50221_sl_create_session(struct en50221_session_layer *sl,
  156. int slot_id, uint8_t connection_id,
  157. uint32_t resource_id,
  158. en50221_sl_resource_callback callback,
  159. void *arg)
  160. {
  161. // lookup next free session_id:
  162. pthread_mutex_lock(&sl->global_lock);
  163. int session_number =
  164. en50221_sl_alloc_new_session(sl, resource_id, slot_id,
  165. connection_id, callback, arg);
  166. if (session_number == -1) {
  167. pthread_mutex_unlock(&sl->global_lock);
  168. return -1;
  169. }
  170. pthread_mutex_unlock(&sl->global_lock);
  171. // make up the header
  172. uint8_t hdr[8];
  173. hdr[0] = ST_CREATE_SESSION;
  174. hdr[1] = 6;
  175. hdr[2] = resource_id >> 24;
  176. hdr[3] = resource_id >> 16;
  177. hdr[4] = resource_id >> 8;
  178. hdr[5] = resource_id;
  179. hdr[6] = session_number >> 8;
  180. hdr[7] = session_number;
  181. // send this command
  182. if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 8)) {
  183. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  184. if (sl->sessions[session_number].state == S_STATE_IN_CREATION) {
  185. sl->sessions[session_number].state = S_STATE_IDLE;
  186. }
  187. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  188. sl->error = en50221_tl_get_error(sl->tl);
  189. return -1;
  190. }
  191. // ok.
  192. return session_number;
  193. }
  194. int en50221_sl_destroy_session(struct en50221_session_layer *sl,
  195. uint16_t session_number)
  196. {
  197. if (session_number >= sl->max_sessions) {
  198. sl->error = EN50221ERR_BADSESSIONNUMBER;
  199. return -1;
  200. }
  201. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  202. if (!(sl->sessions[session_number].state & (S_STATE_ACTIVE | S_STATE_IN_DELETION))) {
  203. sl->error = EN50221ERR_BADSESSIONNUMBER;
  204. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  205. return -1;
  206. }
  207. // set the state
  208. sl->sessions[session_number].state = S_STATE_IN_DELETION;
  209. // get essential details
  210. uint8_t slot_id = sl->sessions[session_number].slot_id;
  211. uint8_t connection_id = sl->sessions[session_number].connection_id;
  212. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  213. // sendit
  214. uint8_t hdr[4];
  215. hdr[0] = ST_CLOSE_SESSION_REQ;
  216. hdr[1] = 2;
  217. hdr[2] = session_number >> 8;
  218. hdr[3] = session_number;
  219. if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 4)) {
  220. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  221. if (sl->sessions[session_number].state == S_STATE_IN_DELETION) {
  222. sl->sessions[session_number].state = S_STATE_IDLE;
  223. }
  224. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  225. sl->error = en50221_tl_get_error(sl->tl);
  226. return -1;
  227. }
  228. return 0;
  229. }
  230. int en50221_sl_send_data(struct en50221_session_layer *sl,
  231. uint16_t session_number,
  232. uint8_t *data,
  233. uint16_t data_length)
  234. {
  235. if (session_number >= sl->max_sessions) {
  236. sl->error = EN50221ERR_BADSESSIONNUMBER;
  237. return -1;
  238. }
  239. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  240. if (sl->sessions[session_number].state != S_STATE_ACTIVE) {
  241. sl->error = EN50221ERR_BADSESSIONNUMBER;
  242. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  243. return -1;
  244. }
  245. // get essential details
  246. uint8_t slot_id = sl->sessions[session_number].slot_id;
  247. uint8_t connection_id = sl->sessions[session_number].connection_id;
  248. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  249. // sendit
  250. struct iovec iov[2];
  251. uint8_t hdr[4];
  252. hdr[0] = ST_SESSION_NUMBER;
  253. hdr[1] = 2;
  254. hdr[2] = session_number >> 8;
  255. hdr[3] = session_number;
  256. iov[0].iov_base = hdr;
  257. iov[0].iov_len = 4;
  258. iov[1].iov_base = data;
  259. iov[1].iov_len = data_length;
  260. if (en50221_tl_send_datav(sl->tl, slot_id, connection_id, iov, 2)) {
  261. sl->error = en50221_tl_get_error(sl->tl);
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. int en50221_sl_send_datav(struct en50221_session_layer *sl,
  267. uint16_t session_number,
  268. struct iovec *vector,
  269. int iov_count)
  270. {
  271. if (session_number >= sl->max_sessions) {
  272. sl->error = EN50221ERR_BADSESSIONNUMBER;
  273. return -1;
  274. }
  275. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  276. if (sl->sessions[session_number].state != S_STATE_ACTIVE) {
  277. sl->error = EN50221ERR_BADSESSIONNUMBER;
  278. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  279. return -1;
  280. }
  281. if (iov_count > 9) {
  282. sl->error = EN50221ERR_IOVLIMIT;
  283. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  284. return -1;
  285. }
  286. uint8_t slot_id = sl->sessions[session_number].slot_id;
  287. uint8_t connection_id = sl->sessions[session_number].connection_id;
  288. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  289. // make up the header
  290. struct iovec out_iov[10];
  291. uint8_t hdr[4];
  292. hdr[0] = ST_SESSION_NUMBER;
  293. hdr[1] = 2;
  294. hdr[2] = session_number >> 8;
  295. hdr[3] = session_number;
  296. out_iov[0].iov_base = hdr;
  297. out_iov[0].iov_len = 4;
  298. // make up the data
  299. memcpy(&out_iov[1], vector, iov_count * sizeof(struct iovec));
  300. // send this command
  301. if (en50221_tl_send_datav(sl->tl, slot_id, connection_id, out_iov, iov_count + 1)) {
  302. sl->error = en50221_tl_get_error(sl->tl);
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. int en50221_sl_broadcast_data(struct en50221_session_layer *sl,
  308. int slot_id, uint32_t resource_id,
  309. uint8_t *data, uint16_t data_length)
  310. {
  311. uint32_t i;
  312. for (i = 0; i < sl->max_sessions; i++) {
  313. pthread_mutex_lock(&sl->sessions[i].session_lock);
  314. if (sl->sessions[i].state != S_STATE_ACTIVE) {
  315. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  316. continue;
  317. }
  318. if ((slot_id != -1)
  319. && (slot_id != sl->sessions[i].slot_id)) {
  320. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  321. continue;
  322. }
  323. if (sl->sessions[i].resource_id == resource_id) {
  324. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  325. en50221_sl_send_data(sl, i, data, data_length);
  326. } else {
  327. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  328. }
  329. }
  330. return 0;
  331. }
  332. static void en50221_sl_handle_open_session_request(struct en50221_session_layer *sl,
  333. uint8_t *data,
  334. uint32_t data_length,
  335. uint8_t slot_id,
  336. uint8_t connection_id)
  337. {
  338. // check
  339. if (data_length < 5) {
  340. print(LOG_LEVEL, ERROR, 1,
  341. "Received data with invalid length from module on slot %02x\n",
  342. slot_id);
  343. return;
  344. }
  345. if (data[0] != 4) {
  346. print(LOG_LEVEL, ERROR, 1,
  347. "Received data with invalid length from module on slot %02x\n",
  348. slot_id);
  349. return;
  350. }
  351. // get the resource id
  352. uint32_t requested_resource_id =
  353. (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
  354. // get lookup callback details
  355. pthread_mutex_lock(&sl->setcallback_lock);
  356. en50221_sl_lookup_callback lcb = sl->lookup;
  357. void *lcb_arg = sl->lookup_arg;
  358. pthread_mutex_unlock(&sl->setcallback_lock);
  359. // first of all, lookup this resource id
  360. int status = S_STATUS_CLOSE_NO_RES;
  361. en50221_sl_resource_callback resource_callback = NULL;
  362. void *resource_arg = NULL;
  363. uint32_t connected_resource_id;
  364. if (lcb) {
  365. status =
  366. lcb(lcb_arg, slot_id, requested_resource_id,
  367. &resource_callback, &resource_arg,
  368. &connected_resource_id);
  369. switch (status) {
  370. case 0:
  371. status = S_STATUS_OPEN;
  372. break;
  373. case -1:
  374. status = S_STATUS_CLOSE_NO_RES;
  375. break;
  376. case -2:
  377. status = S_STATUS_CLOSE_RES_LOW_VERSION;
  378. break;
  379. case -3:
  380. status = S_STATUS_CLOSE_RES_UNAVAILABLE;
  381. break;
  382. }
  383. }
  384. // if we found it, get a new session for it
  385. int session_number = -1;
  386. if (status == S_STATUS_OPEN) {
  387. // lookup next free session_id:
  388. pthread_mutex_lock(&sl->global_lock);
  389. session_number =
  390. en50221_sl_alloc_new_session(sl, connected_resource_id,
  391. slot_id, connection_id,
  392. resource_callback,
  393. resource_arg);
  394. pthread_mutex_unlock(&sl->global_lock);
  395. if (session_number == -1) {
  396. status = S_STATUS_CLOSE_NO_RES;
  397. } else {
  398. // inform upper layers/ check availability
  399. pthread_mutex_lock(&sl->setcallback_lock);
  400. en50221_sl_session_callback cb = sl->session;
  401. void *cb_arg = sl->session_arg;
  402. pthread_mutex_unlock(&sl->setcallback_lock);
  403. if (cb) {
  404. if (cb(cb_arg, S_SCALLBACK_REASON_CAMCONNECTING,
  405. slot_id, session_number,
  406. connected_resource_id)) {
  407. status = S_STATUS_CLOSE_RES_BUSY;
  408. }
  409. } else {
  410. status = S_STATUS_CLOSE_RES_UNAVAILABLE;
  411. }
  412. }
  413. }
  414. // send response
  415. uint8_t hdr[9];
  416. hdr[0] = ST_OPEN_SESSION_RES;
  417. hdr[1] = 7;
  418. hdr[2] = status;
  419. hdr[3] = connected_resource_id >> 24;
  420. hdr[4] = connected_resource_id >> 16;
  421. hdr[5] = connected_resource_id >> 8;
  422. hdr[6] = connected_resource_id;
  423. hdr[7] = session_number >> 8;
  424. hdr[8] = session_number;
  425. if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 9)) {
  426. print(LOG_LEVEL, ERROR, 1,
  427. "Transport layer error %i occurred\n",
  428. en50221_tl_get_error(sl->tl));
  429. status = S_STATUS_CLOSE_NO_RES;
  430. // fallthrough
  431. }
  432. // inform upper layers what happened
  433. if (session_number != -1) {
  434. // setup session state apppropriately from upper layer response
  435. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  436. if (status != S_STATUS_OPEN) {
  437. sl->sessions[session_number].state = S_STATE_IDLE;
  438. } else {
  439. sl->sessions[session_number].state = S_STATE_ACTIVE;
  440. }
  441. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  442. // tell upper layers
  443. if (sl->sessions[session_number].state == S_STATE_ACTIVE) {
  444. pthread_mutex_lock(&sl->setcallback_lock);
  445. en50221_sl_session_callback cb = sl->session;
  446. void *cb_arg = sl->session_arg;
  447. pthread_mutex_unlock(&sl->setcallback_lock);
  448. if (status == S_STATUS_OPEN) {
  449. if (cb)
  450. cb(cb_arg,
  451. S_SCALLBACK_REASON_CAMCONNECTED,
  452. slot_id, session_number,
  453. connected_resource_id);
  454. } else {
  455. sl->sessions[session_number].state =
  456. S_STATE_IDLE;
  457. if (cb)
  458. cb(cb_arg,
  459. S_SCALLBACK_REASON_CAMCONNECTFAIL,
  460. slot_id, session_number,
  461. connected_resource_id);
  462. }
  463. }
  464. }
  465. }
  466. static void en50221_sl_handle_close_session_request(struct en50221_session_layer *sl,
  467. uint8_t * data,
  468. uint32_t data_length,
  469. uint8_t slot_id,
  470. uint8_t connection_id)
  471. {
  472. // check
  473. if (data_length < 3) {
  474. print(LOG_LEVEL, ERROR, 1,
  475. "Received data with invalid length from module on slot %02x\n",
  476. slot_id);
  477. return;
  478. }
  479. if (data[0] != 2) {
  480. print(LOG_LEVEL, ERROR, 1,
  481. "Received data with invalid length from module on slot %02x\n",
  482. slot_id);
  483. return;
  484. }
  485. // extract session number
  486. uint16_t session_number = (data[1] << 8) | data[2];
  487. // check session number is ok
  488. uint8_t code = 0x00;
  489. uint32_t resource_id = 0;
  490. if (session_number >= sl->max_sessions) {
  491. code = 0xF0; // session close error
  492. print(LOG_LEVEL, ERROR, 1, "Received bad session id %i\n",
  493. slot_id);
  494. } else {
  495. pthread_mutex_lock(&sl->sessions[session_number].
  496. session_lock);
  497. if (slot_id != sl->sessions[session_number].slot_id) {
  498. print(LOG_LEVEL, ERROR, 1,
  499. "Received unexpected session on invalid slot %i\n",
  500. slot_id);
  501. code = 0xF0; // session close error
  502. }
  503. if (connection_id != sl->sessions[session_number].connection_id) {
  504. print(LOG_LEVEL, ERROR, 1,
  505. "Received unexpected session on invalid slot %i\n",
  506. slot_id);
  507. code = 0xF0; // session close error
  508. }
  509. if (!(sl->sessions[session_number].state & (S_STATE_ACTIVE | S_STATE_IN_DELETION))) {
  510. print(LOG_LEVEL, ERROR, 1,
  511. "Received unexpected session on invalid slot %i\n",
  512. slot_id);
  513. code = 0xF0; // session close error
  514. }
  515. if (code == 0x00) {
  516. sl->sessions[session_number].state = S_STATE_IDLE;
  517. code = 0x00; // close ok
  518. }
  519. resource_id = sl->sessions[session_number].resource_id;
  520. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  521. }
  522. // make up the response
  523. uint8_t hdr[5];
  524. hdr[0] = ST_CLOSE_SESSION_RES;
  525. hdr[1] = 3;
  526. hdr[2] = code;
  527. hdr[3] = session_number >> 8;
  528. hdr[4] = session_number;
  529. // sendit
  530. if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 5)) {
  531. print(LOG_LEVEL, ERROR, 1,
  532. "Transport layer reports error %i on slot %i\n",
  533. en50221_tl_get_error(sl->tl), slot_id);
  534. }
  535. // callback to announce destruction to resource if it was ok
  536. if (code == 0x00) {
  537. pthread_mutex_lock(&sl->setcallback_lock);
  538. en50221_sl_session_callback cb = sl->session;
  539. void *cb_arg = sl->session_arg;
  540. pthread_mutex_unlock(&sl->setcallback_lock);
  541. if (cb)
  542. cb(cb_arg, S_SCALLBACK_REASON_CLOSE, slot_id,
  543. session_number, resource_id);
  544. }
  545. }
  546. static void en50221_sl_handle_create_session_response(struct en50221_session_layer *sl,
  547. uint8_t * data,
  548. uint32_t data_length,
  549. uint8_t slot_id,
  550. uint8_t connection_id)
  551. {
  552. // check
  553. if (data_length < 8) {
  554. print(LOG_LEVEL, ERROR, 1,
  555. "Received data with invalid length from module on slot %02x\n",
  556. slot_id);
  557. return;
  558. }
  559. if (data[0] != 7) {
  560. print(LOG_LEVEL, ERROR, 1,
  561. "Received data with invalid length from module on slot %02x\n",
  562. slot_id);
  563. return;
  564. }
  565. // extract session number
  566. uint16_t session_number = (data[5] << 8) | data[6];
  567. // check session number is ok
  568. if (session_number >= sl->max_sessions) {
  569. print(LOG_LEVEL, ERROR, 1, "Received bad session id %i\n",
  570. slot_id);
  571. return;
  572. }
  573. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  574. if (slot_id != sl->sessions[session_number].slot_id) {
  575. print(LOG_LEVEL, ERROR, 1,
  576. "Received unexpected session on invalid slot %i\n",
  577. slot_id);
  578. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  579. return;
  580. }
  581. if (connection_id != sl->sessions[session_number].connection_id) {
  582. print(LOG_LEVEL, ERROR, 1,
  583. "Received unexpected session on invalid slot %i\n",
  584. slot_id);
  585. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  586. return;
  587. }
  588. if (sl->sessions[session_number].state != S_STATE_IN_CREATION) {
  589. print(LOG_LEVEL, ERROR, 1,
  590. "Received unexpected session on invalid slot %i\n",
  591. slot_id);
  592. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  593. return;
  594. }
  595. // extract status
  596. if (data[1] != S_STATUS_OPEN) {
  597. print(LOG_LEVEL, ERROR, 1,
  598. "Session creation failed 0x%02x\n", data[1]);
  599. sl->sessions[session_number].state = S_STATE_IDLE;
  600. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  601. // inform upper layers
  602. pthread_mutex_lock(&sl->setcallback_lock);
  603. en50221_sl_session_callback cb = sl->session;
  604. void *cb_arg = sl->session_arg;
  605. pthread_mutex_unlock(&sl->setcallback_lock);
  606. if (cb)
  607. cb(cb_arg, S_SCALLBACK_REASON_CONNECTFAIL, slot_id,
  608. session_number,
  609. sl->sessions[session_number].resource_id);
  610. return;
  611. }
  612. // set it active
  613. sl->sessions[session_number].state = S_STATE_ACTIVE;
  614. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  615. // inform upper layers
  616. pthread_mutex_lock(&sl->setcallback_lock);
  617. en50221_sl_session_callback cb = sl->session;
  618. void *cb_arg = sl->session_arg;
  619. pthread_mutex_unlock(&sl->setcallback_lock);
  620. if (cb)
  621. cb(cb_arg, S_SCALLBACK_REASON_CONNECTED, slot_id,
  622. session_number,
  623. sl->sessions[session_number].resource_id);
  624. }
  625. static void en50221_sl_handle_close_session_response(struct en50221_session_layer *sl,
  626. uint8_t *data,
  627. uint32_t data_length,
  628. uint8_t slot_id,
  629. uint8_t connection_id)
  630. {
  631. // check
  632. if (data_length < 5) {
  633. print(LOG_LEVEL, ERROR, 1,
  634. "Received data with invalid length from module on slot %02x\n",
  635. slot_id);
  636. return;
  637. }
  638. if (data[0] != 4) {
  639. print(LOG_LEVEL, ERROR, 1,
  640. "Received data with invalid length from module on slot %02x\n",
  641. slot_id);
  642. return;
  643. }
  644. // extract session number
  645. uint16_t session_number = (data[2] << 8) | data[3];
  646. // check session number is ok
  647. if (session_number >= sl->max_sessions) {
  648. print(LOG_LEVEL, ERROR, 1, "Received bad session id %i\n", slot_id);
  649. return;
  650. }
  651. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  652. if (slot_id != sl->sessions[session_number].slot_id) {
  653. print(LOG_LEVEL, ERROR, 1,
  654. "Received unexpected session on invalid slot %i\n",
  655. slot_id);
  656. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  657. return;
  658. }
  659. if (connection_id != sl->sessions[session_number].connection_id) {
  660. print(LOG_LEVEL, ERROR, 1,
  661. "Received unexpected session on invalid slot %i\n",
  662. slot_id);
  663. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  664. return;
  665. }
  666. if (sl->sessions[session_number].state != S_STATE_IN_DELETION) {
  667. print(LOG_LEVEL, ERROR, 1,
  668. "Received unexpected session on invalid slot %i\n",
  669. slot_id);
  670. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  671. return;
  672. }
  673. // extract status
  674. if (data[1] != 0x00) {
  675. print(LOG_LEVEL, ERROR, 1, "Session close failed 0x%02x\n", data[1]);
  676. // just fallthrough anyway
  677. }
  678. // completed
  679. sl->sessions[session_number].state = S_STATE_IDLE;
  680. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  681. }
  682. static void en50221_sl_handle_session_package(struct en50221_session_layer *sl,
  683. uint8_t *data,
  684. uint32_t data_length,
  685. uint8_t slot_id,
  686. uint8_t connection_id)
  687. {
  688. // check
  689. if (data_length < 3) {
  690. print(LOG_LEVEL, ERROR, 1,
  691. "Received data with invalid length from module on slot %i\n",
  692. slot_id);
  693. return;
  694. }
  695. if (data[0] != 2) {
  696. print(LOG_LEVEL, ERROR, 1,
  697. "Received data with invalid length from module on slot %i\n",
  698. slot_id);
  699. return;
  700. }
  701. // get session number
  702. uint16_t session_number = (data[1] << 8) | data[2];
  703. // check it
  704. if (session_number >= sl->max_sessions) {
  705. print(LOG_LEVEL, ERROR, 1,
  706. "Received data with bad session_number from module on slot %i\n",
  707. slot_id);
  708. return;
  709. }
  710. pthread_mutex_lock(&sl->sessions[session_number].session_lock);
  711. if (slot_id != sl->sessions[session_number].slot_id) {
  712. print(LOG_LEVEL, ERROR, 1,
  713. "Received unexpected session on invalid slot %i\n",
  714. slot_id);
  715. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  716. return;
  717. }
  718. if (connection_id != sl->sessions[session_number].connection_id) {
  719. print(LOG_LEVEL, ERROR, 1,
  720. "Received unexpected session on invalid slot %i\n",
  721. slot_id);
  722. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  723. return;
  724. }
  725. if (sl->sessions[session_number].state != S_STATE_ACTIVE) {
  726. print(LOG_LEVEL, ERROR, 1,
  727. "Received data with bad session_number from module on slot %i\n",
  728. slot_id);
  729. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  730. return;
  731. }
  732. en50221_sl_resource_callback cb = sl->sessions[session_number].callback;
  733. void *cb_arg = sl->sessions[session_number].callback_arg;
  734. uint32_t resource_id = sl->sessions[session_number].resource_id;
  735. pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
  736. // there can be > 1 APDU following the package - all for the same session/resource_id tho.
  737. data += 3;
  738. data_length -= 3;
  739. while (data_length) {
  740. // check length field
  741. if (data_length < 3) {
  742. print(LOG_LEVEL, ERROR, 1,
  743. "Received invalid sized session package from slot %i\n",
  744. slot_id);
  745. return;
  746. }
  747. // parse the APDU's length field
  748. int length_field_len;
  749. uint16_t asn_data_length;
  750. if ((length_field_len = asn_1_decode(&asn_data_length, data + 3, data_length - 3)) < 0) {
  751. print(LOG_LEVEL, ERROR, 1,
  752. "Received invalid sized session package from slot %i\n",
  753. slot_id);
  754. return;
  755. }
  756. uint32_t apdu_length = 3 + length_field_len + asn_data_length;
  757. // check there is enough data
  758. if (apdu_length > data_length) {
  759. print(LOG_LEVEL, ERROR, 1,
  760. "Received invalid sized session package from slot %i\n",
  761. slot_id);
  762. return;
  763. }
  764. // pass the APDU up to the higher layers
  765. if (cb)
  766. cb(cb_arg, slot_id, session_number, resource_id, data, apdu_length);
  767. // next!
  768. data += apdu_length;
  769. data_length -= apdu_length;
  770. }
  771. }
  772. static void en50221_sl_transport_callback(void *arg, int reason,
  773. uint8_t *data,
  774. uint32_t data_length,
  775. uint8_t slot_id,
  776. uint8_t connection_id)
  777. {
  778. struct en50221_session_layer *sl =
  779. (struct en50221_session_layer *) arg;
  780. uint32_t i;
  781. // deal with the reason for this callback
  782. switch (reason) {
  783. case T_CALLBACK_REASON_DATA:
  784. // fallthrough into rest of this function
  785. break;
  786. case T_CALLBACK_REASON_CONNECTIONOPEN:
  787. {
  788. pthread_mutex_lock(&sl->setcallback_lock);
  789. en50221_sl_session_callback cb = sl->session;
  790. void *cb_arg = sl->session_arg;
  791. pthread_mutex_unlock(&sl->setcallback_lock);
  792. if (cb)
  793. cb(cb_arg, S_SCALLBACK_REASON_TC_CONNECT,
  794. slot_id, connection_id, 0);
  795. return;
  796. }
  797. case T_CALLBACK_REASON_CAMCONNECTIONOPEN:
  798. {
  799. pthread_mutex_lock(&sl->setcallback_lock);
  800. en50221_sl_session_callback cb = sl->session;
  801. void *cb_arg = sl->session_arg;
  802. pthread_mutex_unlock(&sl->setcallback_lock);
  803. if (cb)
  804. cb(cb_arg,
  805. S_SCALLBACK_REASON_TC_CAMCONNECT,
  806. slot_id, connection_id, 0);
  807. return;
  808. }
  809. case T_CALLBACK_REASON_CONNECTIONCLOSE:
  810. {
  811. pthread_mutex_lock(&sl->setcallback_lock);
  812. en50221_sl_session_callback cb = sl->session;
  813. void *cb_arg = sl->session_arg;
  814. pthread_mutex_unlock(&sl->setcallback_lock);
  815. for (i = 0; i < sl->max_sessions; i++) {
  816. pthread_mutex_lock(&sl->sessions[i].session_lock);
  817. if (sl->sessions[i].state == S_STATE_IDLE) {
  818. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  819. continue;
  820. }
  821. if (sl->sessions[i].connection_id != connection_id) {
  822. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  823. continue;
  824. }
  825. sl->sessions[i].state = S_STATE_IDLE;
  826. uint8_t _slot_id = sl->sessions[i].slot_id;
  827. uint32_t resource_id = sl->sessions[i].resource_id;
  828. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  829. if (cb)
  830. cb(cb_arg, S_SCALLBACK_REASON_CLOSE, _slot_id, i, resource_id);
  831. }
  832. return;
  833. }
  834. case T_CALLBACK_REASON_SLOTCLOSE:
  835. {
  836. pthread_mutex_lock(&sl->setcallback_lock);
  837. en50221_sl_session_callback cb = sl->session;
  838. void *cb_arg = sl->session_arg;
  839. pthread_mutex_unlock(&sl->setcallback_lock);
  840. for (i = 0; i < sl->max_sessions; i++) {
  841. pthread_mutex_lock(&sl->sessions[i].session_lock);
  842. if (sl->sessions[i].state == S_STATE_IDLE) {
  843. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  844. continue;
  845. }
  846. if (sl->sessions[i].slot_id != slot_id) {
  847. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  848. continue;
  849. }
  850. sl->sessions[i].state = S_STATE_IDLE;
  851. uint32_t resource_id = sl->sessions[i].resource_id;
  852. pthread_mutex_unlock(&sl->sessions[i].session_lock);
  853. if (cb)
  854. cb(cb_arg, S_SCALLBACK_REASON_CLOSE, slot_id, i, resource_id);
  855. }
  856. return;
  857. }
  858. }
  859. // sanity check data length
  860. if (data_length < 1) {
  861. print(LOG_LEVEL, ERROR, 1,
  862. "Received data with invalid length from module on slot %i\n",
  863. slot_id);
  864. return;
  865. }
  866. // deal with the data
  867. uint8_t spdu_tag = data[0];
  868. switch (spdu_tag) {
  869. case ST_OPEN_SESSION_REQ:
  870. en50221_sl_handle_open_session_request(sl, data + 1,
  871. data_length - 1,
  872. slot_id,
  873. connection_id);
  874. break;
  875. case ST_CLOSE_SESSION_REQ:
  876. en50221_sl_handle_close_session_request(sl, data + 1,
  877. data_length - 1,
  878. slot_id,
  879. connection_id);
  880. break;
  881. case ST_SESSION_NUMBER:
  882. en50221_sl_handle_session_package(sl, data + 1,
  883. data_length - 1, slot_id,
  884. connection_id);
  885. break;
  886. case ST_CREATE_SESSION_RES:
  887. en50221_sl_handle_create_session_response(sl, data + 1,
  888. data_length - 1,
  889. slot_id,
  890. connection_id);
  891. break;
  892. case ST_CLOSE_SESSION_RES:
  893. en50221_sl_handle_close_session_response(sl, data + 1,
  894. data_length - 1,
  895. slot_id,
  896. connection_id);
  897. break;
  898. default:
  899. print(LOG_LEVEL, ERROR, 1,
  900. "Received unknown session tag %02x from module on slot %i",
  901. spdu_tag, slot_id);
  902. break;
  903. }
  904. }
  905. static int en50221_sl_alloc_new_session(struct en50221_session_layer *sl,
  906. uint32_t resource_id,
  907. uint8_t slot_id,
  908. uint8_t connection_id,
  909. en50221_sl_resource_callback
  910. callback, void *arg)
  911. {
  912. int session_number = -1;
  913. uint32_t i;
  914. for (i = 1; i < sl->max_sessions; i++) {
  915. if (sl->sessions[i].state == S_STATE_IDLE) {
  916. session_number = i;
  917. break;
  918. }
  919. }
  920. if (session_number == -1) {
  921. sl->error = EN50221ERR_OUTOFSESSIONS;
  922. return -1;
  923. }
  924. // setup the session
  925. sl->sessions[session_number].state = S_STATE_IN_CREATION;
  926. sl->sessions[session_number].resource_id = resource_id;
  927. sl->sessions[session_number].slot_id = slot_id;
  928. sl->sessions[session_number].connection_id = connection_id;
  929. sl->sessions[session_number].callback = callback;
  930. sl->sessions[session_number].callback_arg = arg;
  931. // ok
  932. return session_number;
  933. }