Added mutex for multiple SPI devices on the same SPI bus

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Revision:
45:50ab13d8f2dc
Parent:
42:bd2c631a031a
--- a/cc3000_socket.cpp	Sun Oct 13 11:46:21 2013 +0200
+++ b/cc3000_socket.cpp	Wed Nov 06 17:56:25 2013 +0100
@@ -46,25 +46,21 @@
 namespace mbed_cc3000 {
 
 cc3000_socket::cc3000_socket(cc3000_simple_link &simplelink, cc3000_hci &hci, cc3000_event &event)
-    : _simple_link(simplelink), _hci(hci), _event(event)
-{
+    : _simple_link(simplelink), _hci(hci), _event(event) {
 
 }
 
-cc3000_socket::~cc3000_socket()
-{
+cc3000_socket::~cc3000_socket() {
 
 }
 
 int32_t cc3000_socket::HostFlowControlConsumeBuff(int32_t sd) {
 #ifndef SEND_NON_BLOCKING
     /* wait in busy loop */
-    do
-    {
+    do {
         // When the last transmission failed, return the last failure reason.
         // Note that the buffer will not be allocated in this case
-        if (_simple_link.get_transmit_error() != 0)
-        {
+        if (_simple_link.get_transmit_error() != 0) {
             errno = _simple_link.get_transmit_error();
             _simple_link.set_transmit_error(0);
             return errno;
@@ -72,7 +68,7 @@
 
         if(SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd))
             return -1;
-    } while(0 == _simple_link.get_number_free_buffers());
+    } while (0 == _simple_link.get_number_free_buffers());
 
     uint16_t free_buffer = _simple_link.get_number_free_buffers();
     free_buffer--;
@@ -83,24 +79,20 @@
 
     // When the last transmission failed, return the last failure reason.
     // Note that the buffer will not be allocated in this case
-    if (_simple_link.get_transmit_error() != 0)
-    {
+    if (_simple_link.get_transmit_error() != 0) {
         errno = _simple_link.get_transmit_error();
         _simple_link.set_transmit_error(0);
         return errno;
     }
-    if(SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd))
+    if (SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd))
         return -1;
 
     // If there are no available buffers, return -2. It is recommended to use
     // select or receive to see if there is any buffer occupied with received data
     // If so, call receive() to release the buffer.
-    if(0 == _simple_link.get_number_free_buffers())
-    {
+    if (0 == _simple_link.get_number_free_buffers()) {
         return -2;
-    }
-    else
-    {
+    } else {
         uint16_t free_buffer = _simple_link.get_number_free_buffers();
         free_buffer--;
         _simple_link.set_number_free_buffers(free_buffer);
@@ -133,14 +125,14 @@
 
     _event.set_socket_active_status(ret, SOCKET_STATUS_ACTIVE);
 
-    return(ret);
+    return ret;
 }
 
 int32_t cc3000_socket::closesocket(int32_t sd) {
     int32_t ret;
     uint8_t *ptr, *args;
 
-    while(_simple_link.get_number_free_buffers() != SOCKET_MAX_FREE_BUFFERS);
+    while (_simple_link.get_number_free_buffers() != SOCKET_MAX_FREE_BUFFERS);
     ret = EFAIL;
     ptr = _simple_link.get_transmit_buffer();
     args = (ptr + HEADERS_SIZE_CMD);
@@ -158,7 +150,7 @@
     // since 'close' call may result in either OK (and then it closed) or error, mark this socket as invalid
     _event.set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
 
-    return(ret);
+    return ret;
 }
 
 int32_t cc3000_socket::accept(int32_t sd, sockaddr *addr, socklen_t *addrlen) {
@@ -187,16 +179,13 @@
     ret = errno;
 
     // if succeeded, iStatus = new socket descriptor. otherwise - error number
-    if(M_IS_VALID_SD(ret))
-    {
+    if(M_IS_VALID_SD(ret)) {
         _event.set_socket_active_status(ret, SOCKET_STATUS_ACTIVE);
-    }
-    else
-    {
+    } else {
         _event.set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
     }
 
-    return(ret);
+    return ret;
 }
 
 int32_t cc3000_socket::bind(int32_t sd, const sockaddr *addr, int32_t addrlen) {
@@ -223,7 +212,7 @@
 
     errno = ret;
 
-    return(ret);
+    return ret;
 }
 
 int32_t cc3000_socket::listen(int32_t sd, int32_t backlog) {
@@ -279,12 +268,9 @@
     tBsdSelectRecvParams tParams;
     uint32_t is_blocking;
 
-    if( timeout == NULL)
-    {
+    if (timeout == NULL) {
         is_blocking = 1; /* blocking , infinity timeout */
-    }
-    else
-    {
+    } else {
         is_blocking = 0; /* no blocking, timeout */
     }
 
@@ -303,10 +289,8 @@
     args = UINT32_TO_STREAM(args, ((writesds) ? *(uint32_t*)writesds : 0));
     args = UINT32_TO_STREAM(args, ((exceptsds) ? *(uint32_t*)exceptsds : 0));
 
-    if (timeout)
-    {
-        if ( 0 == timeout->tv_sec && timeout->tv_usec < SELECT_TIMEOUT_MIN_MICRO_SECONDS)
-        {
+    if (timeout) {
+        if ( 0 == timeout->tv_sec && timeout->tv_usec < SELECT_TIMEOUT_MIN_MICRO_SECONDS) {
             timeout->tv_usec = SELECT_TIMEOUT_MIN_MICRO_SECONDS;
         }
         args = UINT32_TO_STREAM(args, timeout->tv_sec);
@@ -320,30 +304,24 @@
     _event.simplelink_wait_event(HCI_EVNT_SELECT, &tParams);
 
     // Update actually read FD
-    if (tParams.iStatus >= 0)
-    {
-        if (readsds)
-        {
+    if (tParams.iStatus >= 0) {
+        if (readsds) {
             memcpy(readsds, &tParams.uiRdfd, sizeof(tParams.uiRdfd));
         }
 
-        if (writesds)
-        {
+        if (writesds) {
             memcpy(writesds, &tParams.uiWrfd, sizeof(tParams.uiWrfd));
         }
 
-        if (exceptsds)
-        {
+        if (exceptsds) {
             memcpy(exceptsds, &tParams.uiExfd, sizeof(tParams.uiExfd));
         }
 
         return(tParams.iStatus);
 
-    }
-    else
-    {
+    } else {
         errno = tParams.iStatus;
-        return(-1);
+        return -1;
     }
 }
 
@@ -365,14 +343,11 @@
     // Since we are in blocking state - wait for event complete
     _event.simplelink_wait_event(HCI_CMND_GETSOCKOPT, &tRetParams);
 
-    if (((int8_t)tRetParams.iStatus) >= 0)
-    {
+    if (((int8_t)tRetParams.iStatus) >= 0) {
         *optlen = 4;
         memcpy(optval, tRetParams.ucOptValue, 4);
         return (0);
-    }
-    else
-    {
+    } else {
         errno = tRetParams.iStatus;
         return errno;
     }
@@ -397,8 +372,7 @@
     _event.simplelink_wait_event(opcode, &tSocketReadEvent);
 
     // In case the number of bytes is more then zero - read data
-    if (tSocketReadEvent.iNumberOfBytes > 0)
-    {
+    if (tSocketReadEvent.iNumberOfBytes > 0) {
         // Wait for the data in a synchronous way. Here we assume that the bug is
         // big enough to store also parameters of receive from too....
         _event.simplelink_wait_data((uint8_t *)buf, (uint8_t *)from, (uint8_t *)fromlen);
@@ -425,8 +399,7 @@
     tBsdReadReturnParams tSocketSendEvent;
 
     // Check the bsd_arguments
-    if (0 != (res = HostFlowControlConsumeBuff(sd)))
-    {
+    if (0 != (res = HostFlowControlConsumeBuff(sd))) {
         return res;
     }
 
@@ -442,7 +415,7 @@
     // Update the offset of data and parameters according to the command
     switch(opcode)
     {
-    case HCI_CMND_SENDTO:
+        case HCI_CMND_SENDTO:
         {
             addr_offset = len + sizeof(len) + sizeof(len);
             addrlen = 8;
@@ -451,7 +424,7 @@
             break;
         }
 
-    case HCI_CMND_SEND:
+        case HCI_CMND_SEND:
         {
             tolen = 0;
             to = NULL;
@@ -460,7 +433,7 @@
             break;
         }
 
-    default:
+        default:
         {
             break;
         }
@@ -472,8 +445,7 @@
     args = UINT32_TO_STREAM(args, len);
     args = UINT32_TO_STREAM(args, flags);
 
-    if (opcode == HCI_CMND_SENDTO)
-    {
+    if (opcode == HCI_CMND_SENDTO) {
         args = UINT32_TO_STREAM(args, addr_offset);
         args = UINT32_TO_STREAM(args, addrlen);
     }
@@ -482,8 +454,7 @@
     ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)buf), len);
 
     // In case we are using SendTo, copy the to parameters
-    if (opcode == HCI_CMND_SENDTO)
-    {
+    if (opcode == HCI_CMND_SENDTO) {
         ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)to), tolen);
     }
 
@@ -509,8 +480,7 @@
     int32_t ret;
      uint8_t *pTxBuffer, *pArgs;
 
-    if (device_service_name_length > MDNS_DEVICE_SERVICE_MAX_LENGTH)
-    {
+    if (device_service_name_length > MDNS_DEVICE_SERVICE_MAX_LENGTH) {
         return EFAIL;
     }
 
@@ -540,8 +510,7 @@
 
     errno = EFAIL;
 
-    if (name_length > HOSTNAME_MAX_LENGTH)
-    {
+    if (name_length > HOSTNAME_MAX_LENGTH) {
         return errno;
     }
 
@@ -587,12 +556,9 @@
     // Since we are in blocking state - wait for event complete
     _event.simplelink_wait_event(HCI_CMND_SETSOCKOPT, &ret);
 
-    if (ret >= 0)
-    {
+    if (ret >= 0) {
         return (0);
-    }
-    else
-    {
+    } else {
         errno = ret;
         return ret;
     }
@@ -600,39 +566,39 @@
 
 #endif
 
-char * cc3000_socket::inet_ntoa_r(uint32_t s_addr, char *buf, int buflen)
+char* cc3000_socket::inet_ntoa_r(uint32_t s_addr, char *buf, int buflen)
 {
-  char inv[3];
-  char *rp;
-  uint8_t *ap;
-  uint8_t rem;
-  uint8_t n;
-  uint8_t i;
-  int len = 0;
+    char inv[3];
+    char *rp;
+    uint8_t *ap;
+    uint8_t rem;
+    uint8_t n;
+    uint8_t i;
+    int len = 0;
 
-  rp = buf;
-  ap = (uint8_t *)&s_addr;
-  for(n = 0; n < 4; n++) {
-    i = 0;
-    do {
-      rem = *ap % (uint8_t)10;
-      *ap /= (uint8_t)10;
-      inv[i++] = '0' + rem;
-    } while(*ap);
-    while(i--) {
-      if (len++ >= buflen) {
-        return NULL;
-      }
-      *rp++ = inv[i];
+    rp = buf;
+    ap = (uint8_t *)&s_addr;
+    for (n = 0; n < 4; n++) {
+        i = 0;
+        do {
+            rem = *ap % (uint8_t)10;
+            *ap /= (uint8_t)10;
+            inv[i++] = '0' + rem;
+        } while(*ap);
+        while(i--) {
+            if (len++ >= buflen) {
+                return NULL;
+            }
+            *rp++ = inv[i];
+        }
+        if (len++ >= buflen) {
+            return NULL;
+        }
+        *rp++ = '.';
+        ap++;
     }
-    if (len++ >= buflen) {
-      return NULL;
-    }
-    *rp++ = '.';
-    ap++;
-  }
-  *--rp = 0;
-  return buf;
+    *--rp = 0;
+    return buf;
 }
 
-} /* end of cc3000 namespace */
+} // mbed_cc3000 namespace