Redbear Nano firmware using Sandroide and showing how to send custom strings and parameters to the Android application
Dependencies: BLE_API MbedJSONValue mbed nRF51822
Fork of BLE_RedBearNano-SAndroidEDevice by
Queue.h@4:2ef9b332fa7c, 2017-10-06 (annotated)
- Committer:
- giowild
- Date:
- Fri Oct 06 10:05:45 2017 +0000
- Revision:
- 4:2ef9b332fa7c
- Parent:
- 2:6c45738bba43
Send Custom strings/parameters from Nano to smartphone
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giowild | 2:6c45738bba43 | 1 | // downloaded from: https://gist.github.com/ArnonEilat/4471278 |
giowild | 2:6c45738bba43 | 2 | |
giowild | 2:6c45738bba43 | 3 | #include <stdlib.h> |
giowild | 2:6c45738bba43 | 4 | #include <stdio.h> |
giowild | 2:6c45738bba43 | 5 | |
giowild | 2:6c45738bba43 | 6 | #define TRUE 1 |
giowild | 2:6c45738bba43 | 7 | #define FALSE 0 |
giowild | 2:6c45738bba43 | 8 | #define QUEUE_STRING_LENGTH 40 |
giowild | 2:6c45738bba43 | 9 | |
giowild | 2:6c45738bba43 | 10 | /* a link in the queue, holds the info and point to the next Node*/ |
giowild | 2:6c45738bba43 | 11 | typedef struct { |
giowild | 2:6c45738bba43 | 12 | int length; |
giowild | 2:6c45738bba43 | 13 | char payload[QUEUE_STRING_LENGTH-1]; // null-terminated string, max 256 characters long, this may be JSON or any other format, depending on the application |
giowild | 2:6c45738bba43 | 14 | } DATA; |
giowild | 2:6c45738bba43 | 15 | |
giowild | 2:6c45738bba43 | 16 | typedef struct Node_t { |
giowild | 2:6c45738bba43 | 17 | DATA data; |
giowild | 2:6c45738bba43 | 18 | struct Node_t *prev; |
giowild | 2:6c45738bba43 | 19 | } NODE; |
giowild | 2:6c45738bba43 | 20 | |
giowild | 2:6c45738bba43 | 21 | /* the HEAD of the Queue, hold the amount of node's that are in the queue*/ |
giowild | 2:6c45738bba43 | 22 | typedef struct Queue { |
giowild | 2:6c45738bba43 | 23 | NODE *head; |
giowild | 2:6c45738bba43 | 24 | NODE *tail; |
giowild | 2:6c45738bba43 | 25 | int size; |
giowild | 2:6c45738bba43 | 26 | int limit; |
giowild | 2:6c45738bba43 | 27 | } Queue; |
giowild | 2:6c45738bba43 | 28 | |
giowild | 2:6c45738bba43 | 29 | Queue *ConstructQueue(int limit); |
giowild | 2:6c45738bba43 | 30 | void DestructQueue(Queue *queue); |
giowild | 2:6c45738bba43 | 31 | int Enqueue(Queue *pQueue, NODE *item); |
giowild | 2:6c45738bba43 | 32 | NODE *Dequeue(Queue *pQueue); |
giowild | 2:6c45738bba43 | 33 | int isEmpty(Queue* pQueue); |
giowild | 2:6c45738bba43 | 34 | |
giowild | 2:6c45738bba43 | 35 | Queue *ConstructQueue(int limit) { |
giowild | 2:6c45738bba43 | 36 | Queue *queue = (Queue*) malloc(sizeof (Queue)); |
giowild | 2:6c45738bba43 | 37 | if (queue == NULL) { |
giowild | 2:6c45738bba43 | 38 | return NULL; |
giowild | 2:6c45738bba43 | 39 | } |
giowild | 2:6c45738bba43 | 40 | if (limit <= 0) { |
giowild | 2:6c45738bba43 | 41 | limit = 65535; |
giowild | 2:6c45738bba43 | 42 | } |
giowild | 2:6c45738bba43 | 43 | queue->limit = limit; |
giowild | 2:6c45738bba43 | 44 | queue->size = 0; |
giowild | 2:6c45738bba43 | 45 | queue->head = NULL; |
giowild | 2:6c45738bba43 | 46 | queue->tail = NULL; |
giowild | 2:6c45738bba43 | 47 | |
giowild | 2:6c45738bba43 | 48 | return queue; |
giowild | 2:6c45738bba43 | 49 | } |
giowild | 2:6c45738bba43 | 50 | |
giowild | 2:6c45738bba43 | 51 | void DestructQueue(Queue *queue) { |
giowild | 2:6c45738bba43 | 52 | NODE *pN; |
giowild | 2:6c45738bba43 | 53 | while (!isEmpty(queue)) { |
giowild | 2:6c45738bba43 | 54 | pN = Dequeue(queue); |
giowild | 2:6c45738bba43 | 55 | free(pN); |
giowild | 2:6c45738bba43 | 56 | } |
giowild | 2:6c45738bba43 | 57 | free(queue); |
giowild | 2:6c45738bba43 | 58 | } |
giowild | 2:6c45738bba43 | 59 | |
giowild | 2:6c45738bba43 | 60 | int Enqueue(Queue *pQueue, NODE *item) { |
giowild | 2:6c45738bba43 | 61 | /* Bad parameter */ |
giowild | 2:6c45738bba43 | 62 | if ((pQueue == NULL) || (item == NULL)) { |
giowild | 2:6c45738bba43 | 63 | return FALSE; |
giowild | 2:6c45738bba43 | 64 | } |
giowild | 2:6c45738bba43 | 65 | // if(pQueue->limit != 0) |
giowild | 2:6c45738bba43 | 66 | if (pQueue->size >= pQueue->limit) { |
giowild | 2:6c45738bba43 | 67 | return FALSE; |
giowild | 2:6c45738bba43 | 68 | } |
giowild | 2:6c45738bba43 | 69 | /*the queue is empty*/ |
giowild | 2:6c45738bba43 | 70 | item->prev = NULL; |
giowild | 2:6c45738bba43 | 71 | if (pQueue->size == 0) { |
giowild | 2:6c45738bba43 | 72 | pQueue->head = item; |
giowild | 2:6c45738bba43 | 73 | pQueue->tail = item; |
giowild | 2:6c45738bba43 | 74 | |
giowild | 2:6c45738bba43 | 75 | } else { |
giowild | 2:6c45738bba43 | 76 | /*adding item to the end of the queue*/ |
giowild | 2:6c45738bba43 | 77 | pQueue->tail->prev = item; |
giowild | 2:6c45738bba43 | 78 | pQueue->tail = item; |
giowild | 2:6c45738bba43 | 79 | } |
giowild | 2:6c45738bba43 | 80 | pQueue->size++; |
giowild | 2:6c45738bba43 | 81 | return TRUE; |
giowild | 2:6c45738bba43 | 82 | } |
giowild | 2:6c45738bba43 | 83 | |
giowild | 2:6c45738bba43 | 84 | NODE * Dequeue(Queue *pQueue) { |
giowild | 2:6c45738bba43 | 85 | /*the queue is empty or bad param*/ |
giowild | 2:6c45738bba43 | 86 | NODE *item; |
giowild | 2:6c45738bba43 | 87 | if (isEmpty(pQueue)) |
giowild | 2:6c45738bba43 | 88 | return NULL; |
giowild | 2:6c45738bba43 | 89 | item = pQueue->head; |
giowild | 2:6c45738bba43 | 90 | pQueue->head = (pQueue->head)->prev; |
giowild | 2:6c45738bba43 | 91 | pQueue->size--; |
giowild | 2:6c45738bba43 | 92 | return item; |
giowild | 2:6c45738bba43 | 93 | } |
giowild | 2:6c45738bba43 | 94 | |
giowild | 2:6c45738bba43 | 95 | int isEmpty(Queue* pQueue) { |
giowild | 2:6c45738bba43 | 96 | if (pQueue == NULL) { |
giowild | 2:6c45738bba43 | 97 | return FALSE; |
giowild | 2:6c45738bba43 | 98 | } |
giowild | 2:6c45738bba43 | 99 | if (pQueue->size == 0) { |
giowild | 2:6c45738bba43 | 100 | return TRUE; |
giowild | 2:6c45738bba43 | 101 | } else { |
giowild | 2:6c45738bba43 | 102 | return FALSE; |
giowild | 2:6c45738bba43 | 103 | } |
giowild | 2:6c45738bba43 | 104 | } |
giowild | 2:6c45738bba43 | 105 | /* |
giowild | 2:6c45738bba43 | 106 | int main() { |
giowild | 2:6c45738bba43 | 107 | int i; |
giowild | 2:6c45738bba43 | 108 | Queue *pQ = ConstructQueue(7); |
giowild | 2:6c45738bba43 | 109 | NODE *pN; |
giowild | 2:6c45738bba43 | 110 | |
giowild | 2:6c45738bba43 | 111 | for (i = 0; i < 9; i++) { |
giowild | 2:6c45738bba43 | 112 | pN = (NODE*) malloc(sizeof (NODE)); |
giowild | 2:6c45738bba43 | 113 | pN->data.info = 100 + i; |
giowild | 2:6c45738bba43 | 114 | Enqueue(pQ, pN); |
giowild | 2:6c45738bba43 | 115 | } |
giowild | 2:6c45738bba43 | 116 | |
giowild | 2:6c45738bba43 | 117 | while (!isEmpty(pQ)) { |
giowild | 2:6c45738bba43 | 118 | pN = Dequeue(pQ); |
giowild | 2:6c45738bba43 | 119 | printf("\nDequeued: %d", pN->data); |
giowild | 2:6c45738bba43 | 120 | free(pN); |
giowild | 2:6c45738bba43 | 121 | } |
giowild | 2:6c45738bba43 | 122 | DestructQueue(pQ); |
giowild | 2:6c45738bba43 | 123 | return (EXIT_SUCCESS); |
giowild | 2:6c45738bba43 | 124 | } |
giowild | 2:6c45738bba43 | 125 | |
giowild | 2:6c45738bba43 | 126 | */ |