Added mutex for multiple SPI devices on the same SPI bus

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
Kojto
Date:
Thu Sep 19 07:55:14 2013 +0000
Revision:
0:615c697c33b0
Child:
4:15b58c119a0a
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:615c697c33b0 1 /* Copyright (C) 2012 mbed.org, MIT License
Kojto 0:615c697c33b0 2 *
Kojto 0:615c697c33b0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 0:615c697c33b0 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Kojto 0:615c697c33b0 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Kojto 0:615c697c33b0 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Kojto 0:615c697c33b0 7 * furnished to do so, subject to the following conditions:
Kojto 0:615c697c33b0 8 *
Kojto 0:615c697c33b0 9 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 0:615c697c33b0 10 * substantial portions of the Software.
Kojto 0:615c697c33b0 11 *
Kojto 0:615c697c33b0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 0:615c697c33b0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 0:615c697c33b0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 0:615c697c33b0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 0:615c697c33b0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 0:615c697c33b0 17 */
Kojto 0:615c697c33b0 18 #include "Socket/Socket.h"
Kojto 0:615c697c33b0 19 #include "Socket/Endpoint.h"
Kojto 0:615c697c33b0 20 #include <cstring>
Kojto 0:615c697c33b0 21
Kojto 0:615c697c33b0 22 #include "cc3000.h"
Kojto 0:615c697c33b0 23
Kojto 0:615c697c33b0 24 using std::memset;
Kojto 0:615c697c33b0 25
Kojto 0:615c697c33b0 26 Endpoint::Endpoint() {
Kojto 0:615c697c33b0 27 _cc3000_module = cc3000::get_instance();
Kojto 0:615c697c33b0 28 if (_cc3000_module == NULL) {
Kojto 0:615c697c33b0 29 error("Endpoint constructor error: no cc3000 instance available!\r\n");
Kojto 0:615c697c33b0 30 }
Kojto 0:615c697c33b0 31 reset_address();
Kojto 0:615c697c33b0 32 }
Kojto 0:615c697c33b0 33 Endpoint::~Endpoint() {}
Kojto 0:615c697c33b0 34
Kojto 0:615c697c33b0 35 void Endpoint::reset_address(void) {
Kojto 0:615c697c33b0 36 _ipAddress[0] = '\0';
Kojto 0:615c697c33b0 37 std::memset(&_remote_host,0, sizeof(sockaddr));
Kojto 0:615c697c33b0 38 }
Kojto 0:615c697c33b0 39
Kojto 0:615c697c33b0 40 int Endpoint::set_address(const char* host, const int port) {
Kojto 0:615c697c33b0 41 reset_address();
Kojto 0:615c697c33b0 42
Kojto 0:615c697c33b0 43 // IP Address
Kojto 0:615c697c33b0 44 char address[5];
Kojto 0:615c697c33b0 45 char *p_address = address;
Kojto 0:615c697c33b0 46
Kojto 0:615c697c33b0 47 // Dot-decimal notation
Kojto 0:615c697c33b0 48 int result = std::sscanf(host, "%3u.%3u.%3u.%3u",
Kojto 0:615c697c33b0 49 (unsigned int*)&address[0], (unsigned int*)&address[1],
Kojto 0:615c697c33b0 50 (unsigned int*)&address[2], (unsigned int*)&address[3]);
Kojto 0:615c697c33b0 51
Kojto 0:615c697c33b0 52 if (result != 4) {
Kojto 0:615c697c33b0 53 //Resolve DNS address or populate hard-coded IP address
Kojto 0:615c697c33b0 54 uint32_t address_integer;
Kojto 0:615c697c33b0 55 _cc3000_module->_socket.get_host_by_name((uint8_t *)host, strlen(host) , &address_integer);
Kojto 0:615c697c33b0 56
Kojto 0:615c697c33b0 57 address[0] = (address_integer >> 24);
Kojto 0:615c697c33b0 58 address[1] = (address_integer >> 16);
Kojto 0:615c697c33b0 59 address[2] = (address_integer >> 8);
Kojto 0:615c697c33b0 60 address[3] = (address_integer >> 0);
Kojto 0:615c697c33b0 61 sprintf(_ipAddress,"%3u.%3u.%3u.%3u", address[0],address[1],address[2],address[3]);
Kojto 0:615c697c33b0 62 _remote_host.sin_addr.s_addr = address_integer;
Kojto 0:615c697c33b0 63 } else {
Kojto 0:615c697c33b0 64 std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4);
Kojto 0:615c697c33b0 65 }
Kojto 0:615c697c33b0 66
Kojto 0:615c697c33b0 67 /* store address*/
Kojto 0:615c697c33b0 68 _remote_host.sin_family = AF_INET;
Kojto 0:615c697c33b0 69
Kojto 0:615c697c33b0 70 // Set port
Kojto 0:615c697c33b0 71 _remote_host.sin_port = htons(port);
Kojto 0:615c697c33b0 72
Kojto 0:615c697c33b0 73 return 0;
Kojto 0:615c697c33b0 74 }
Kojto 0:615c697c33b0 75
Kojto 0:615c697c33b0 76 static char *inet_ntoa_r(const in_addr addr, char *buf, int buflen)
Kojto 0:615c697c33b0 77 {
Kojto 0:615c697c33b0 78 uint32_t s_addr;
Kojto 0:615c697c33b0 79 char inv[3];
Kojto 0:615c697c33b0 80 char *rp;
Kojto 0:615c697c33b0 81 uint8_t *ap;
Kojto 0:615c697c33b0 82 uint8_t rem;
Kojto 0:615c697c33b0 83 uint8_t n;
Kojto 0:615c697c33b0 84 uint8_t i;
Kojto 0:615c697c33b0 85 int len = 0;
Kojto 0:615c697c33b0 86
Kojto 0:615c697c33b0 87 s_addr = addr.s_addr;
Kojto 0:615c697c33b0 88
Kojto 0:615c697c33b0 89 rp = buf;
Kojto 0:615c697c33b0 90 ap = (uint8_t *)&s_addr;
Kojto 0:615c697c33b0 91 for(n = 0; n < 4; n++) {
Kojto 0:615c697c33b0 92 i = 0;
Kojto 0:615c697c33b0 93 do {
Kojto 0:615c697c33b0 94 rem = *ap % (uint8_t)10;
Kojto 0:615c697c33b0 95 *ap /= (uint8_t)10;
Kojto 0:615c697c33b0 96 inv[i++] = '0' + rem;
Kojto 0:615c697c33b0 97 } while(*ap);
Kojto 0:615c697c33b0 98 while(i--) {
Kojto 0:615c697c33b0 99 if (len++ >= buflen) {
Kojto 0:615c697c33b0 100 return NULL;
Kojto 0:615c697c33b0 101 }
Kojto 0:615c697c33b0 102 *rp++ = inv[i];
Kojto 0:615c697c33b0 103 }
Kojto 0:615c697c33b0 104 if (len++ >= buflen) {
Kojto 0:615c697c33b0 105 return NULL;
Kojto 0:615c697c33b0 106 }
Kojto 0:615c697c33b0 107 *rp++ = '.';
Kojto 0:615c697c33b0 108 ap++;
Kojto 0:615c697c33b0 109 }
Kojto 0:615c697c33b0 110 *--rp = 0;
Kojto 0:615c697c33b0 111 return buf;
Kojto 0:615c697c33b0 112 }
Kojto 0:615c697c33b0 113
Kojto 0:615c697c33b0 114 char* Endpoint::get_address() {
Kojto 0:615c697c33b0 115 if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0))
Kojto 0:615c697c33b0 116 inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress));
Kojto 0:615c697c33b0 117 return _ipAddress;
Kojto 0:615c697c33b0 118 }
Kojto 0:615c697c33b0 119
Kojto 0:615c697c33b0 120
Kojto 0:615c697c33b0 121 int Endpoint::get_port() {
Kojto 0:615c697c33b0 122 return ntohs(_remote_host.sin_port);
Kojto 0:615c697c33b0 123 }