BLE fota Lamp Demo
Dependencies: mbed-rtos mbed mbed_fota_fan_control
Fork of mbed_fota_fan_control by
ext_fota/BleMsgHandler.cpp@3:1e70387e1337, 2015-06-23 (annotated)
- Committer:
- dudnwjs
- Date:
- Tue Jun 23 06:32:40 2015 +0000
- Revision:
- 3:1e70387e1337
- Parent:
- 2:dba344c91bce
- Child:
- 4:60be78a172c2
Add feature : Msg Sender, Msg Receiver (using Serial);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudnwjs | 0:a1f6b1ba8a1e | 1 | #include "BleMsgHandler.h" |
dudnwjs | 3:1e70387e1337 | 2 | #include "dialog_fota_config.h" |
dudnwjs | 0:a1f6b1ba8a1e | 3 | |
dudnwjs | 0:a1f6b1ba8a1e | 4 | namespace sevencore_fota{ |
dudnwjs | 0:a1f6b1ba8a1e | 5 | |
dudnwjs | 0:a1f6b1ba8a1e | 6 | BleMsgHandler::BleMsgHandler(Serial *_device) |
dudnwjs | 0:a1f6b1ba8a1e | 7 | { |
dudnwjs | 0:a1f6b1ba8a1e | 8 | print_flag = 0; |
dudnwjs | 0:a1f6b1ba8a1e | 9 | device = _device; |
dudnwjs | 2:dba344c91bce | 10 | SerialM = new SerialManager(_device); |
dudnwjs | 3:1e70387e1337 | 11 | MsgQ = new MsgQueue(512); |
dudnwjs | 3:1e70387e1337 | 12 | memset(recv_msg,0,512); |
dudnwjs | 0:a1f6b1ba8a1e | 13 | } |
dudnwjs | 0:a1f6b1ba8a1e | 14 | |
dudnwjs | 0:a1f6b1ba8a1e | 15 | BleMsgHandler::BleMsgHandler(Serial *_device, Serial *_hostpc) |
dudnwjs | 0:a1f6b1ba8a1e | 16 | { |
dudnwjs | 0:a1f6b1ba8a1e | 17 | print_flag = 1; |
dudnwjs | 0:a1f6b1ba8a1e | 18 | device = _device; |
dudnwjs | 0:a1f6b1ba8a1e | 19 | hostpc = _hostpc; |
dudnwjs | 2:dba344c91bce | 20 | SerialM = new SerialManager(_device,_hostpc); |
dudnwjs | 3:1e70387e1337 | 21 | MsgQ = new MsgQueue(512); |
dudnwjs | 3:1e70387e1337 | 22 | memset(recv_msg,0,512); |
dudnwjs | 0:a1f6b1ba8a1e | 23 | } |
dudnwjs | 0:a1f6b1ba8a1e | 24 | |
dudnwjs | 1:5cf3a6c969be | 25 | BleMsgHandler::~BleMsgHandler(void) |
dudnwjs | 1:5cf3a6c969be | 26 | { |
dudnwjs | 2:dba344c91bce | 27 | free(SerialM); |
dudnwjs | 3:1e70387e1337 | 28 | free(MsgQ); |
dudnwjs | 1:5cf3a6c969be | 29 | } |
dudnwjs | 1:5cf3a6c969be | 30 | |
dudnwjs | 0:a1f6b1ba8a1e | 31 | void BleMsgHandler::PrintTitle(void) |
dudnwjs | 0:a1f6b1ba8a1e | 32 | { |
dudnwjs | 0:a1f6b1ba8a1e | 33 | if( print_flag == 1 ) |
dudnwjs | 0:a1f6b1ba8a1e | 34 | hostpc->printf("Sevencore Fota : BleMsg Handler Start\n"); |
dudnwjs | 0:a1f6b1ba8a1e | 35 | } |
dudnwjs | 3:1e70387e1337 | 36 | |
dudnwjs | 3:1e70387e1337 | 37 | void BleMsgHandler::BleMsgAlloc( unsigned short id, |
dudnwjs | 3:1e70387e1337 | 38 | unsigned short dest_id, |
dudnwjs | 3:1e70387e1337 | 39 | unsigned short src_id, |
dudnwjs | 3:1e70387e1337 | 40 | unsigned short data_len, |
dudnwjs | 3:1e70387e1337 | 41 | void *pdata, |
dudnwjs | 3:1e70387e1337 | 42 | uint8_t *msg ) |
dudnwjs | 3:1e70387e1337 | 43 | { |
dudnwjs | 3:1e70387e1337 | 44 | memset(msg,0,sizeof(msg)); |
dudnwjs | 3:1e70387e1337 | 45 | msg[0] = 0x05; |
dudnwjs | 3:1e70387e1337 | 46 | memcpy(&msg[1],&id,sizeof(unsigned short)); |
dudnwjs | 3:1e70387e1337 | 47 | memcpy(&msg[1+1*sizeof(unsigned short)],&dest_id,sizeof(unsigned short)); |
dudnwjs | 3:1e70387e1337 | 48 | memcpy(&msg[1+2*sizeof(unsigned short)],&src_id,sizeof(unsigned short)); |
dudnwjs | 3:1e70387e1337 | 49 | memcpy(&msg[1+3*sizeof(unsigned short)],&data_len,sizeof(unsigned short)); |
dudnwjs | 3:1e70387e1337 | 50 | memcpy(&msg[1+4*sizeof(unsigned short)],pdata,data_len); |
dudnwjs | 3:1e70387e1337 | 51 | } |
dudnwjs | 3:1e70387e1337 | 52 | |
dudnwjs | 3:1e70387e1337 | 53 | int BleMsgHandler::BleSendMsg(uint8_t *msg, unsigned short size) |
dudnwjs | 3:1e70387e1337 | 54 | { |
dudnwjs | 3:1e70387e1337 | 55 | return SerialM->SendToSerial(msg,size); |
dudnwjs | 3:1e70387e1337 | 56 | } |
dudnwjs | 3:1e70387e1337 | 57 | |
dudnwjs | 3:1e70387e1337 | 58 | void BleMsgHandler::BleReceiveMsg(void) |
dudnwjs | 3:1e70387e1337 | 59 | { |
dudnwjs | 3:1e70387e1337 | 60 | int receive_size = -1;//default |
dudnwjs | 3:1e70387e1337 | 61 | while(receive_size == -1) |
dudnwjs | 3:1e70387e1337 | 62 | { |
dudnwjs | 3:1e70387e1337 | 63 | receive_size = SerialM->ReceiveToSerial(recv_msg); |
dudnwjs | 3:1e70387e1337 | 64 | } |
dudnwjs | 3:1e70387e1337 | 65 | |
dudnwjs | 3:1e70387e1337 | 66 | uint8_t *msg; |
dudnwjs | 3:1e70387e1337 | 67 | msg = new uint8_t[receive_size]; |
dudnwjs | 3:1e70387e1337 | 68 | memcpy(msg,recv_msg,receive_size); |
dudnwjs | 3:1e70387e1337 | 69 | memset(recv_msg,0,512); |
dudnwjs | 3:1e70387e1337 | 70 | MsgQ->EnQueue(msg); |
dudnwjs | 3:1e70387e1337 | 71 | } |
dudnwjs | 3:1e70387e1337 | 72 | |
dudnwjs | 3:1e70387e1337 | 73 | void BleMsgHandler::BleMsgHandle(void) |
dudnwjs | 3:1e70387e1337 | 74 | { |
dudnwjs | 3:1e70387e1337 | 75 | if( print_flag == 1) |
dudnwjs | 3:1e70387e1337 | 76 | hostpc->printf("Ble-Massage Handle Function!\n"); |
dudnwjs | 3:1e70387e1337 | 77 | |
dudnwjs | 3:1e70387e1337 | 78 | uint8_t *msg; |
dudnwjs | 3:1e70387e1337 | 79 | msg = (uint8_t*)MsgQ->DeQueue(); |
dudnwjs | 3:1e70387e1337 | 80 | |
dudnwjs | 3:1e70387e1337 | 81 | ble_hdr msg_hdr; |
dudnwjs | 3:1e70387e1337 | 82 | memcpy(&msg_hdr, &msg[1], sizeof(msg_hdr)); |
dudnwjs | 3:1e70387e1337 | 83 | |
dudnwjs | 3:1e70387e1337 | 84 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 85 | hostpc->printf(" handle msg : id(%d), dst(%d), src(%d), len(%d) !\n", |
dudnwjs | 3:1e70387e1337 | 86 | msg_hdr.bType, msg_hdr.bDstid, msg_hdr.bSrcid, msg_hdr.bLength); |
dudnwjs | 3:1e70387e1337 | 87 | |
dudnwjs | 3:1e70387e1337 | 88 | if (msg_hdr.bDstid != TASK_GTL) |
dudnwjs | 3:1e70387e1337 | 89 | return; |
dudnwjs | 3:1e70387e1337 | 90 | |
dudnwjs | 3:1e70387e1337 | 91 | } |
dudnwjs | 3:1e70387e1337 | 92 | |
dudnwjs | 3:1e70387e1337 | 93 | |
dudnwjs | 3:1e70387e1337 | 94 | |
dudnwjs | 0:a1f6b1ba8a1e | 95 | |
dudnwjs | 0:a1f6b1ba8a1e | 96 | }//namespace |