asdasdasdasd

Dependencies:   Audio EthernetInterface mbed-rtos mbed

Fork of FRDM_TCP_v4_copy by Tigaresi

Committer:
irsanjul
Date:
Fri Aug 10 09:12:27 2018 +0000
Revision:
11:9280da8e40bd
asdasdas

Who changed what in which revision?

UserRevisionLine numberNew contents of line
irsanjul 11:9280da8e40bd 1 //#include "mbed.h"
irsanjul 11:9280da8e40bd 2 #include "EthernetInterface.h"
irsanjul 11:9280da8e40bd 3 #include "CommChannel.h"
irsanjul 11:9280da8e40bd 4 #include "const_defines.h"
irsanjul 11:9280da8e40bd 5
irsanjul 11:9280da8e40bd 6 extern Serial dbg;
irsanjul 11:9280da8e40bd 7
irsanjul 11:9280da8e40bd 8 //-----------------------------------------------------------------
irsanjul 11:9280da8e40bd 9 extern std::vector<int> RCV_BUFF; // temporary buffer receiving progress
irsanjul 11:9280da8e40bd 10 std::vector<int> RCVD; // buffer completed receiving
irsanjul 11:9280da8e40bd 11
irsanjul 11:9280da8e40bd 12 //-----------------------------------------------------------------
irsanjul 11:9280da8e40bd 13 // Mechanism for receive timeout
irsanjul 11:9280da8e40bd 14 //
irsanjul 11:9280da8e40bd 15 extern int timerRxTOutCnt;
irsanjul 11:9280da8e40bd 16
irsanjul 11:9280da8e40bd 17 //-----------------------------------------------------------------
irsanjul 11:9280da8e40bd 18 // TCP definition
irsanjul 11:9280da8e40bd 19 //
irsanjul 11:9280da8e40bd 20 extern TCPSocketConnection socket;
irsanjul 11:9280da8e40bd 21
irsanjul 11:9280da8e40bd 22 //-----------------------------------------------------------------
irsanjul 11:9280da8e40bd 23 // Wrapper class
irsanjul 11:9280da8e40bd 24 //
irsanjul 11:9280da8e40bd 25 CommChannel::CommChannel()
irsanjul 11:9280da8e40bd 26 {
irsanjul 11:9280da8e40bd 27 // dbg.printf("<CommChannel>");
irsanjul 11:9280da8e40bd 28 }
irsanjul 11:9280da8e40bd 29
irsanjul 11:9280da8e40bd 30 CommChannel::~CommChannel()
irsanjul 11:9280da8e40bd 31 {
irsanjul 11:9280da8e40bd 32
irsanjul 11:9280da8e40bd 33 }
irsanjul 11:9280da8e40bd 34
irsanjul 11:9280da8e40bd 35 void CommChannel::connect(const std::string &host)
irsanjul 11:9280da8e40bd 36 {
irsanjul 11:9280da8e40bd 37 // Connect to Server
irsanjul 11:9280da8e40bd 38 int timeout = 0;
irsanjul 11:9280da8e40bd 39 while (socket.connect(host.c_str(), 5002) < 0) {
irsanjul 11:9280da8e40bd 40 dbg.printf(RED"Unable to connect to (%s) on port (%d)\r\n"DEF, host.c_str(), 5002);
irsanjul 11:9280da8e40bd 41 timeout++;
irsanjul 11:9280da8e40bd 42 if(timeout > 60)break;
irsanjul 11:9280da8e40bd 43 wait(1);
irsanjul 11:9280da8e40bd 44 }
irsanjul 11:9280da8e40bd 45 }
irsanjul 11:9280da8e40bd 46
irsanjul 11:9280da8e40bd 47 int CommChannel::dataAvailable()
irsanjul 11:9280da8e40bd 48 {
irsanjul 11:9280da8e40bd 49 return RCVD.size();
irsanjul 11:9280da8e40bd 50 }
irsanjul 11:9280da8e40bd 51
irsanjul 11:9280da8e40bd 52 std::vector<int> CommChannel::read()
irsanjul 11:9280da8e40bd 53 {
irsanjul 11:9280da8e40bd 54 std::vector<int> result;
irsanjul 11:9280da8e40bd 55
irsanjul 11:9280da8e40bd 56 for(int i=0; i<RCVD.size(); i++)
irsanjul 11:9280da8e40bd 57 result.push_back(RCVD[i]);
irsanjul 11:9280da8e40bd 58
irsanjul 11:9280da8e40bd 59 RCVD.clear();
irsanjul 11:9280da8e40bd 60 return result;
irsanjul 11:9280da8e40bd 61 }
irsanjul 11:9280da8e40bd 62
irsanjul 11:9280da8e40bd 63 void CommChannel::write(const std::vector<int> &s)
irsanjul 11:9280da8e40bd 64 {
irsanjul 11:9280da8e40bd 65 char sData[s.size()];
irsanjul 11:9280da8e40bd 66
irsanjul 11:9280da8e40bd 67 for(int i=0; i<s.size(); i++)
irsanjul 11:9280da8e40bd 68 {
irsanjul 11:9280da8e40bd 69 sData[i] = s[i] & 0x0ff;
irsanjul 11:9280da8e40bd 70 }
irsanjul 11:9280da8e40bd 71
irsanjul 11:9280da8e40bd 72 socket.send_all(sData, sizeof(sData));
irsanjul 11:9280da8e40bd 73 }
irsanjul 11:9280da8e40bd 74
irsanjul 11:9280da8e40bd 75 void CommChannel::write(char *s, int sz)
irsanjul 11:9280da8e40bd 76 {
irsanjul 11:9280da8e40bd 77 socket.send_all(s, sz);
irsanjul 11:9280da8e40bd 78 }
irsanjul 11:9280da8e40bd 79
irsanjul 11:9280da8e40bd 80 void CommChannel::Tick10ms()
irsanjul 11:9280da8e40bd 81 {
irsanjul 11:9280da8e40bd 82 timerRxTOutCnt++;
irsanjul 11:9280da8e40bd 83 if(timerRxTOutCnt >= 10)
irsanjul 11:9280da8e40bd 84 {
irsanjul 11:9280da8e40bd 85 timerRxTOutCnt = 0;
irsanjul 11:9280da8e40bd 86
irsanjul 11:9280da8e40bd 87 if(RCV_BUFF.size() > 0)
irsanjul 11:9280da8e40bd 88 {
irsanjul 11:9280da8e40bd 89 for(int i=0; i<RCV_BUFF.size(); i++)
irsanjul 11:9280da8e40bd 90 {
irsanjul 11:9280da8e40bd 91 RCVD.push_back(RCV_BUFF[i]);
irsanjul 11:9280da8e40bd 92 // dbg.printf("[%02X]", RCV_BUFF[i] & 0x0FF);
irsanjul 11:9280da8e40bd 93 }
irsanjul 11:9280da8e40bd 94 RCV_BUFF.clear();
irsanjul 11:9280da8e40bd 95 }
irsanjul 11:9280da8e40bd 96 }
irsanjul 11:9280da8e40bd 97 }
irsanjul 11:9280da8e40bd 98
irsanjul 11:9280da8e40bd 99 //-----------------------------------------------------------------