void *write_serial(void *_bleCentral) { int n, l; char msg[STD_BUF_SIZE]; memset(msg, 0, STD_BUF_SIZE); BLE_Central_t *bleCentral = (BLE_Central_t *)_bleCentral; datagram_t datagram; debug(1, "Write thread started\n"); while(bleCentral->_run){ if(queueCount(&bleCentral->txQueue) == 0) { usleep(STD_WAIT_TIME); continue; } dequeue(&bleCentral->txQueue, &datagram); COM_compose_datagram(&datagram, msg, &l); n = write(bleCentral->fd, msg, l); updateTxStat(1, n); debug(2, "Message sent (%d bytes) ", n); print_b_array(msg, l, 0); pretty_print_datagram(&datagram); usleep(50*STD_WAIT_TIME); } debug(1, "Write thread exiting\n"); return NULL; }
void *RxComParser(void *_bleCentral) { BLE_Central_t *bleCentral = (BLE_Central_t *)_bleCentral; datagram_t datagram; int i, j, data; char buf [30]; long connHandle, opCode; debug(1, "RX COM Parser started\n"); while(bleCentral->_run) { if(queueCount(&bleCentral->rxQueue) == 0) { usleep(STD_WAIT_TIME); continue; } i = 0; connHandle = 0; opCode = 0; dequeue(&bleCentral->rxQueue, &datagram); if(datagram.type == Event) { if((datagram.opcode & 0xFF) == HCI_LE_ExtEvent) { long evtCode = unload_16_bit(datagram.data, &i, 1); char success = unload_8_bit(datagram.data, &i); debug(3, "Event:\t(%02X %02X)\n", (unsigned int)evtCode >> 8 & 0xFF, (unsigned int)evtCode & 0xFF); debug(3, "Status:\t(%02X) %s\n",(unsigned int)success & 0xFF, getSuccessString(success)); switch(evtCode) { case ATT_ErrorRsp: debug(1, "ATT_ErrorResponce return %s\n", getSuccessString(success)); break; case ATT_WriteRsp: debug(1, "ATT_WriteResponce return %s\n", getSuccessString(success)); break; case ATT_HandleValueNotification: debug(1, "ATT_HandleValueNotifiction return %s ", getSuccessString(success)); connHandle = unload_16_bit(datagram.data, &i, 1); char pduLength = unload_8_bit(datagram.data, &i); if(pduLength < 2) break; long handle = unload_16_bit(datagram.data, &i, 1); char str[100]; format_time_of_day(str, datagram.timestamp); debug(1, "from connHandle %04X for handle 0x%04X at %s with data: ", (unsigned int)connHandle & 0xFFFF, (unsigned int)handle & 0xFFFF, str); for(j=0; j<pduLength-2; j++) { //debug(1, "%02X", (unsigned int)datagram.data[i++] & 0xFF); /* HERE WE WRITE INTO THE SHARED MEM */ printf("%2X", (unsigned int)datagram.data[i++] & 0xFF); } debug(1, "\n"); data = ((datagram.data[9]&0xFF)<<8)+(datagram.data[8]&0xFF); //Check if there is more space in the mapped mem. We need Bytes for the //XML string. //check_size_available(bleCentral->mapped_mem, 2); sprintf(buf,"%d",data*2); //append_mm_XMLfile(bleCentral->rt_count,buf,bleCentral->mapped_mem); //bleCentral->rt_count++; if( !gpio_setValue(LED_IND3, led_value) ) { printf("ERROR: Exporting gpio port."); return 0; } led_value=1-led_value; break; case GAP_DeviceInitDone: debug(1, "GAP_DeviceInitDone return %s with MAC addr: ", getSuccessString(success)); for(j=5; j>=0; j--) { bleCentral->MAC[j] = datagram.data[i++]; } for(j=0; j<6; j++) { debug(1, "%02X%c", (unsigned int)bleCentral->MAC[j] & 0xFF, (j<5)?':':'\n'); } break; case GAP_EstablishLink: { debug(1, "GAP_EstablishLink "); debug(1, "%s to device ", (success==0)?"successfully connected":"failed to connect"); i++; // Device address type char connMAC[6]; for(j=5; j>=0; j--) { connMAC[j] = datagram.data[i++]; } for(j=0; j<6; j++) { debug(1, "%02X%c", (unsigned int)connMAC[j] & 0xFF, (j<5)?':':' '); } if(success == HCI_SUCCESS) { /************************************************************************** * If a device with the given MAC address is already defined this will * be returned, otherwise the first unused device handle will be returned **************************************************************************/ BLE_Peripheral_t* device = getNextAvailableDevice(bleCentral, &datagram.data[i]); if(device == NULL) {//No available devices: bail out debug(1, "\nERROR: No connection handles available\n"); break; } memcpy(device->connMAC, connMAC, 6); device->_defined = 1; device->_connected = 1; device->connHandle = (long)datagram.data[i++]; device->connHandle |= (long)datagram.data[i++] << 8; debug(1, "with connHandle 0x%04X\n", device->connHandle); } } break; case GAP_TerminateLink: { debug(1, "GAP_TerminateLink "); connHandle = unload_16_bit(datagram.data, &i, 1); char reason = unload_8_bit(datagram.data, &i); debug(1, "connHandle 0x%04X with reason %s\n", (unsigned int)connHandle & 0xFFFF, getTerminateString(reason)); BLE_Peripheral_t* device = findDeviceByConnHandle(bleCentral, connHandle); device->_connected = 0; break; } case GAP_HCI_ExtentionCommandStatus: debug(1, "GAP_HCI_ExtentionCommandStatus "); opCode = 0; opCode = unload_16_bit(datagram.data, &i, 1); switch(opCode) { case GATT_WriteCharValue: debug(1, "return %s for WriteCharValue\n", getSuccessString(success)); break; case GAP_DeviceInit: debug(1, "return %s for DeviceInit\n", getSuccessString(success)); break; case GAP_EstablishLinkRequest: debug(1, "return %s for EstablishLinkRequest\n", getSuccessString(success)); break; case GAP_TerminateLinkRequest: debug(1, "return %s for TerminateLinkRequest\n", getSuccessString(success)); break; default: debug(1, "return %s for unknown opcode %04X\n", getSuccessString(success), (unsigned int)opCode); } break; default: debug(1, "HCI_LE_ExtEvent OpCode %04X not supported\n", (unsigned int)opCode); } } else { debug(1, "Datagram eventCode not supported: %04X\n", (unsigned int)datagram.opcode); } }
void queuePrint() { int i; for (i = 0; i < queueCount(); i++) { printf("queue[%d] = %d\n", (queue.front + i) % queue.size, queue.array[(queue.front + i) % queue.size]); } }
int main( void ) { QUEUE *queue; QUEUE_NODE *queueNode; void* dataInPtr; void* dataOutPtr; int queue_status; int queue_element_count; queue = createQueue (); //a.print queue status, Empty printf("Test Case(a) : Print queue status, Empty\n\n"); queue_status = emptyQueue(queue); if(queue_status == 1) { printf("queue status : Empty\n"); } else { printf("queue status : not empty\n"); } printf("\n\n"); //b.Dequeue and print data. Should return error printf("Test Case(b) : Dequeue and print data\n\n"); dequeue( queue, &dataOutPtr ); printf("\n\n"); //c. Enqueue data into queue printf("Test Case(c) : Enqueue data into queue : Ann\n\n"); dataInPtr = (char*)calloc((strlen("Ann")+1), sizeof(char) ); strcpy(dataInPtr, "Ann"); enqueue( queue, dataInPtr); //d. Enqueue data into queue printf("Test Case(d) : Enqueue data into queue : Bob\n\n"); dataInPtr = (char*)calloc(strlen("Bob")+1, sizeof(char) ); strcpy(dataInPtr, "Bob"); enqueue( queue, dataInPtr); //e.Print queue status, Empty printf("Test Case(e) : Print queue status, Empty\n\n"); queue_status = emptyQueue(queue); if(queue_status == 1) { printf("queue status : Empty\n"); } else { printf("queue status : not empty\n"); } printf("\n\n"); //f.Print queue status, Full printf("Test Case(f) : Print queue status, Full\n\n"); queue_status = fullQueue( queue ); if(queue_status == 1) { printf("queue status : Full\n"); } else { printf("queue status : not full\n"); } printf("\n\n"); //g. Print data at the front printf("Test Case(g) : Print data at the front\n\n"); queueFront ( queue, &dataOutPtr ); printf("Data at the queue front is : %s\n", queue->front->dataPtr); printf("\n\n"); //h. Print data at the rear printf("Test Case(h) : Print data at the rear\n\n"); queueRear ( queue, &dataOutPtr ); printf("Data at the queue rear is : %s\n", queue->rear->dataPtr); printf("\n\n"); //i. Print entire queue printf("Test Case(i) : Print entire queue\n\n"); printf("The entire queue is : "); queueNode = queue->front; while(queueNode != NULL) { printf("%s ", queueNode->dataPtr); queueNode = queueNode->link; } printf("\n\n"); //j.Print number of element in queue printf("Test Case(j) : Print number of element in queue\n\n"); queue_element_count = queueCount( queue ); printf("total no of element in queue is %d\n", queue_element_count); printf("\n\n"); //k.Dequeue and print data printf("Test Case(k) : Dequeue and print data\n\n"); dequeue( queue, &dataOutPtr ); printf("after dequeueing we got : %s\n", dataOutPtr); free(dataOutPtr); printf("\n\n"); //l.Dequeue and print data printf("Test Case(l) : Dequeue and print data\n\n"); dequeue( queue, &dataOutPtr ); printf("after dequeueing we got : %s\n", dataOutPtr); free(dataOutPtr); printf("\n\n"); //m.Dequeue and print data printf("Test Case(m) : Dequeue and print data\n\n"); dequeue( queue, &dataOutPtr ); printf("\n\n"); //n. Enqueue data into queue printf("Test Case(n) : Enqueue and print data : Dan\n\n"); dataInPtr = (char*)calloc(strlen("Dan")+1, sizeof(char) ); strcpy(dataInPtr, "Dan"); enqueue( queue, dataInPtr ); //o. Print data at the front printf("Test Case(o) : Print data at the front\n\n"); queueFront ( queue, &dataOutPtr ); printf("Data at the queue front is : %s\n", queue->front->dataPtr); printf("\n\n"); //p. Print data at the rear printf("Test Case(p) : Print data at the rear\n\n"); queueRear ( queue, &dataOutPtr ); printf("Data at the queue rear is : %s\n", queue->rear->dataPtr); printf("\n\n"); //q. Enqueue data into queue printf("Test Case(q) : Enqueue data into queue : Tom\n\n"); dataInPtr = (char*)calloc(strlen("Tom")+1, sizeof(char) ); strcpy(dataInPtr, "Tom"); enqueue( queue, dataInPtr ); //r. print data at the front printf("Test Case(r) : Print data at the front\n\n"); queueFront( queue, &dataOutPtr ); printf("Data at the queue front is : %s\n", queue->front->dataPtr); printf("\n\n"); //s. Print data at the rear printf("Test Case(s) : Print data at the rear\n\n"); queueRear ( queue, &dataOutPtr ); printf("Data at the queue rear is : %s\n", queue->rear->dataPtr); printf("\n\n"); //t. Destroy queue and quit printf("Test Case(t) : Destroy queue and quit\n\n"); destroyQueue( queue ); #ifdef _MSC_VER printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n"); #endif system("pause"); return 0; }