char queue for WiFi communication

Committer:
williampeers
Date:
Wed Aug 09 01:12:08 2017 +0000
Revision:
1:f9e6627f1f59
Parent:
0:043b1271ed1b
Child:
2:8f54ba4d961f
Nothing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
williampeers 0:043b1271ed1b 1 #include "objectQueue.h"
williampeers 0:043b1271ed1b 2
williampeers 0:043b1271ed1b 3 StrQueue::StrQueue() {
williampeers 0:043b1271ed1b 4 chars = (char*)malloc(sizeof(char[1]));
williampeers 0:043b1271ed1b 5 size = 1;
williampeers 0:043b1271ed1b 6 front = 0;
williampeers 0:043b1271ed1b 7 back = 0;
williampeers 0:043b1271ed1b 8 empty = true;
williampeers 0:043b1271ed1b 9 full = false;
williampeers 0:043b1271ed1b 10 }
williampeers 0:043b1271ed1b 11
williampeers 0:043b1271ed1b 12 StrQueue::StrQueue(int size) {
williampeers 0:043b1271ed1b 13 chars = (char*)malloc(sizeof(char[size]));
williampeers 0:043b1271ed1b 14 size = size;
williampeers 0:043b1271ed1b 15 front = 0;
williampeers 0:043b1271ed1b 16 back = 0;
williampeers 0:043b1271ed1b 17 empty = true;
williampeers 0:043b1271ed1b 18 full = false;
williampeers 0:043b1271ed1b 19 }
williampeers 0:043b1271ed1b 20
williampeers 0:043b1271ed1b 21 StrQueue::~StrQueue(void) {
williampeers 0:043b1271ed1b 22 free(chars);
williampeers 0:043b1271ed1b 23 }
williampeers 0:043b1271ed1b 24
williampeers 0:043b1271ed1b 25 int StrQueue::put(const char* message, int length) {
williampeers 0:043b1271ed1b 26 lock.lock();
williampeers 0:043b1271ed1b 27 int i;
williampeers 0:043b1271ed1b 28 if (length == 0) {
williampeers 0:043b1271ed1b 29 for (i = 0; empty||(front != (back+i)%size); i++) {
williampeers 0:043b1271ed1b 30 if ((chars[back + i] = message[i]) == '\0') {
williampeers 0:043b1271ed1b 31 back = (back + i + 1)%size;
williampeers 0:043b1271ed1b 32 empty = false;
williampeers 0:043b1271ed1b 33 if (front == back) {
williampeers 0:043b1271ed1b 34 full = true;
williampeers 0:043b1271ed1b 35 }
williampeers 0:043b1271ed1b 36 lock.unlock();
williampeers 0:043b1271ed1b 37 return(i);
williampeers 0:043b1271ed1b 38 }
williampeers 0:043b1271ed1b 39 }
williampeers 0:043b1271ed1b 40 if (front == back) {
williampeers 0:043b1271ed1b 41 full = true;
williampeers 0:043b1271ed1b 42 }
williampeers 0:043b1271ed1b 43 lock.unlock();
williampeers 0:043b1271ed1b 44 return(0);
williampeers 0:043b1271ed1b 45 }
williampeers 0:043b1271ed1b 46 else {
williampeers 0:043b1271ed1b 47 if ((back - front)%size <= length) {
williampeers 0:043b1271ed1b 48 lock.unlock();
williampeers 0:043b1271ed1b 49 return(0);
williampeers 0:043b1271ed1b 50 }
williampeers 0:043b1271ed1b 51 for (i = 0; empty||(i <= length); i++) {
williampeers 0:043b1271ed1b 52 chars[back + i] = message[i];
williampeers 0:043b1271ed1b 53 }
williampeers 0:043b1271ed1b 54 back = (back + i + 1)%size;
williampeers 0:043b1271ed1b 55 empty = false;
williampeers 0:043b1271ed1b 56 if (front == back) {
williampeers 0:043b1271ed1b 57 full = true;
williampeers 0:043b1271ed1b 58 }
williampeers 0:043b1271ed1b 59 lock.unlock();
williampeers 0:043b1271ed1b 60 return(i);
williampeers 0:043b1271ed1b 61 }
williampeers 0:043b1271ed1b 62 }
williampeers 0:043b1271ed1b 63
williampeers 0:043b1271ed1b 64 int StrQueue::get(char* out, int length) {
williampeers 0:043b1271ed1b 65 lock.lock();
williampeers 0:043b1271ed1b 66 int i = 0;
williampeers 0:043b1271ed1b 67 if (empty) {
williampeers 0:043b1271ed1b 68 lock.unlock();
williampeers 0:043b1271ed1b 69 return(0);
williampeers 0:043b1271ed1b 70 }
williampeers 0:043b1271ed1b 71 for (i = 0;; i++) {
williampeers 0:043b1271ed1b 72 if (i > length) {
williampeers 0:043b1271ed1b 73 strncpy(out, "", length);
williampeers 0:043b1271ed1b 74 return(0);
williampeers 0:043b1271ed1b 75 }
williampeers 0:043b1271ed1b 76 if ((out[i] = chars[(front + i)%size]) == '\0') {
williampeers 0:043b1271ed1b 77 front = (front + i + 1)%size;
williampeers 0:043b1271ed1b 78 break;
williampeers 0:043b1271ed1b 79 }
williampeers 0:043b1271ed1b 80 }
williampeers 0:043b1271ed1b 81 if (front == back) {
williampeers 0:043b1271ed1b 82 empty = true;
williampeers 0:043b1271ed1b 83 }
williampeers 0:043b1271ed1b 84 lock.unlock();
williampeers 0:043b1271ed1b 85
williampeers 0:043b1271ed1b 86 return(i);
williampeers 0:043b1271ed1b 87 }
williampeers 0:043b1271ed1b 88
williampeers 0:043b1271ed1b 89 int StrQueue::getChars(char* buffer, int size) {
williampeers 0:043b1271ed1b 90 int ret = 0, total = 0;
williampeers 0:043b1271ed1b 91 lock.lock();
williampeers 0:043b1271ed1b 92 if (empty) {return(0);}
williampeers 0:043b1271ed1b 93 while ((ret = get(buffer + ret, size - total)) > 0) {
williampeers 0:043b1271ed1b 94 buffer += ret;
williampeers 1:f9e6627f1f59 95 total++;
williampeers 0:043b1271ed1b 96 ret = 0;
williampeers 0:043b1271ed1b 97 }
williampeers 0:043b1271ed1b 98 lock.unlock();
williampeers 0:043b1271ed1b 99 return(total);
williampeers 0:043b1271ed1b 100 }
williampeers 0:043b1271ed1b 101
williampeers 0:043b1271ed1b 102 bool StrQueue::isfull() {
williampeers 0:043b1271ed1b 103 return(full);
williampeers 0:043b1271ed1b 104 }
williampeers 0:043b1271ed1b 105
williampeers 0:043b1271ed1b 106 bool StrQueue::isEmpty() {
williampeers 0:043b1271ed1b 107 return(empty);
williampeers 0:043b1271ed1b 108 }