Is it possible to send 20bytes of data between BleMesh models using Nimble BleMesh stack on Esp32? - bluetooth-lowenergy

I want to send 20bytes of data between 2 custom BleMesh models which are configured in a Server and Client node respectively.
In the current setup of the firmware, the Server node pings the Client node with 3bytes of data request in a specific opcode blemesh model and the Client node should send 20bytes in response to the same model opcode. This request response should run periodically at an interval of 1s. The Server node receives the 20bytes of information only once and then throws the following error without receiving any further response from the Client Node, although the Client Node receives the requests from the Server Node,
E (84763) NimBLE: bt_mesh_ctl_send: Out of transport buffers
I am using the NrfMesh app to provision the Server and the Client Nodes(both are esp32 dev modules and the Nimble BleMesh stack is used).
I am using the below firmware to communicate between the models of the Server and the Client node.
/*For ServerNode*/
static const struct bt_mesh_model_op vnd_model_op[] = {
{ BT_MESH_MODEL_OP_3(0x03, CID_ESP), 2, vnd_model_recv },
BT_MESH_MODEL_OP_END,
};
static const struct bt_mesh_model_op custom_sensor_op[] = {
{BT_MESH_MODEL_OP_3(0x00, CID_ESP), 0, custom_sensor_get},
{BT_MESH_MODEL_OP_3(0x01, CID_ESP), 0, custom_sensor_set},
{BT_MESH_MODEL_OP_3(0x02, CID_ESP), 0, custom_sensor_stat},
// {BT_MESH_MODEL_OP_3(0x03, CID_ESP), 0, vnd_model_recv},
BT_MESH_MODEL_OP_END,
};
static model_sensor_data_t _server_model_state = {
.device_name = "esp_s",
.temperature = 0,
.pressure = 0,
.tVOC = 0,
};
static struct bt_mesh_model custom_models[] = {
BT_MESH_MODEL_VND(CID_ESP, ESP_BLE_MESH_CUSTOM_SENSOR_MODEL_ID_SERVER,
custom_sensor_op, NULL, &_server_model_state),
BT_MESH_MODEL_VND(CID_ESP, BT_MESH_MODEL_ID_GEN_ONOFF_SRV, vnd_model_op,
&vnd_model_pub, NULL),
};
void ServerDataRequestTask(void *param)
{
struct bt_mesh_model *model = &custom_models[1];
struct bt_mesh_msg_ctx ctx = {
.addr = ClientAddr,
};
while(1)
{
struct os_mbuf *msg = NET_BUF_SIMPLE(3);
bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_3(0x03, CID_ESP));
// os_mbuf_append(msg, "HelloFromClient", strlen("HelloFromClient"));
net_buf_simple_add_le32(msg, 0x554d49);
console_printf("Requesting Data From Client Vendor Model\n");
if (bt_mesh_model_send(&model, &ctx, msg, NULL, NULL)) {
console_printf("#vendor-model-recv: send rsp failed\n");
}
os_mbuf_free_chain(msg);
vTaskDelay(1000 * CONFIG_FREERTOS_HZ / 1000);
}
vTaskDelete(NULL);
}
/*For Client Node*/
static const struct bt_mesh_model_op custom_sensor_op[] = {
{BT_MESH_MODEL_OP_3(0x00, CID_ESP), 0, custom_sensor_g},
{BT_MESH_MODEL_OP_3(0x01, CID_ESP), 0, custom_sensor_s},
{BT_MESH_MODEL_OP_3(0x02, CID_ESP), 0, custom_sensor_status},
// {BT_MESH_MODEL_OP_3(0x03, CID_ESP), 0, vnd_model_recv},
BT_MESH_MODEL_OP_END,
};
static const struct bt_mesh_model_op vnd_model_op[] = {
{ BT_MESH_MODEL_OP_3(0x03, CID_ESP), 2, vnd_model_recv },
BT_MESH_MODEL_OP_END,
};
static struct bt_mesh_model custom_models[] = {
BT_MESH_MODEL_VND(CID_ESP, ESP_BLE_MESH_CUSTOM_SENSOR_MODEL_ID_CLIENT,
custom_sensor_op, NULL, NULL),
BT_MESH_MODEL_VND(CID_ESP, BT_MESH_MODEL_ID_GEN_ONOFF_SRV, vnd_model_op,
&vnd_model_pub, NULL),
};
static void vnd_model_recv(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct os_mbuf *buf)
{
console_printf("Command received from server << ");
console_printf("%s\n", buf->om_data); //bt_hex(buf->om_data, buf->om_len);
sendResponseToServer = 1;
ServerAddr = ctx->addr;
xQueueSend(ResponseQueue, (void*)&sendResponseToServer, (TickType_t)0);
}
void SendResponseTask(void *param)
{
struct bt_mesh_model *model = &custom_models[1];
uint8_t status;
while(1)
{
//vTaskDelay(5);
if(xQueueReceive(ResponseQueue, (void *)&status, (portTickType)1))
{
struct bt_mesh_msg_ctx ctx = {
.addr = ServerAddr,
};
struct os_mbuf *msg = NET_BUF_SIMPLE(3);
bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_3(0x03, CID_ESP));
os_mbuf_append(msg, "HelloFromClient", strlen("HelloFromClient"));
console_printf("Sending reply to server from task\n");
if (bt_mesh_model_send(&model, &ctx, msg, NULL, NULL)) {
console_printf("#vendor-model-recv: send rsp failed\n");
}
os_mbuf_free_chain(msg);
console_printf("Message sent successfully\n");
}
}
vTaskDelete(NULL);
}
Please let me know if the problem statement is possible to handle using the models that I am currently using and any inputs will be highly appreciated.

Related

C++/CX concurrency::create_task with DataReader LoadAsync function cause: 'The operation identifier is not valid.'

I am developing a TCP client module for Hololens 1st gen with native C++/CX.
I create a packet class which contains 1 x (uint32_t) + 7 x (float)
The server is implemented in the synchronized manner, which streams the packets to the client (Hololens) every frame.
Receiver.h
namespace HoloNet {
public ref class holoATCFrameReceiver sealed
{
public:
holoATCFrameReceiver(
_In_ Windows::Networking::Sockets::StreamSocket^ streamSocket);
Windows::Foundation::IAsyncOperation<TrackerFrame^>^ ReceiveAsync();
private:
Concurrency::task<TrackerFrame^> ReceiveTrackerFrameAsync();
private:
Windows::Networking::Sockets::StreamSocket^ _streamSocket;
Windows::Storage::Streams::DataReader^ _reader;
bool _readInProgress;
};
}
and Receiver.cpp
namespace HoloNet {
holoATCFrameReceiver::holoATCFrameReceiver(
Windows::Networking::Sockets::StreamSocket ^ streamSocket)
: _streamSocket(streamSocket)
{
_readInProgress = false;
// reader
_reader = ref new Windows::Storage::Streams::DataReader(
_streamSocket->InputStream);
_reader->UnicodeEncoding =
Windows::Storage::Streams::UnicodeEncoding::Utf8;
_reader->ByteOrder =
Windows::Storage::Streams::ByteOrder::LittleEndian;
}
Windows::Foundation::IAsyncOperation<TrackerFrame^>^ holoATCFrameReceiver::ReceiveAsync()
{
return concurrency::create_async(
[this]()
{
return ReceiveTrackerFrameAsync();
});
}
Concurrency::task<TrackerFrame^>
holoATCFrameReceiver::ReceiveTrackerFrameAsync()
{
return concurrency::create_task(
_reader->LoadAsync(TrackerFrame::TrackerFrameLength)
).then([this](Concurrency::task<unsigned int> headerBytesLoadedTaskResult)
{
headerBytesLoadedTaskResult.wait();
const size_t frameBytesLoaded = headerBytesLoadedTaskResult.get();
if (TrackerFrame::TrackerFrameLength != frameBytesLoaded)
{
throw ref new Platform::FailureException();
}
_readInProgress = true;
TrackerFrame^ header;
TrackerFrame::Read(
_reader,
&header);
dbg::trace(
L"SensorFrameReceiver::ReceiveAsync: sensor %i: t( %f, %f %f ), q( %f, %f, %f, %f)",
header->sensorID,
header->x,
header->y,
header->z,
header->qw,
header->qx,
header->qy,
header->qz);
_readInProgress = false;
return header;
});
}
}
The error throws at * _reader->LoadAsync(TrackerFrame::TrackerFrameLength) *
The receiver is called in the client class like below:
void holoTcpATCClient::OnReceiveATCframe(TrackerFrame^& header) {
std::lock_guard<std::mutex> guard(_socketMutex);
// check read in process
if (_readInProcess)
{
return;
}
_readInProcess = true;
concurrency::create_task(
_receiver->ReceiveAsync()).then(
[&](concurrency::task<TrackerFrame^> sensorFrameTask)
{
sensorFrameTask.wait();
TrackerFrame^ sensorFrame = sensorFrameTask.get()
header = sensorFrame;
});
_readInProcess = false;
}
_receiver->ReceiveAsync() is the begining of the error.
I wonder whether there is a way to pretend this happens by doing some modification on LoadAsync() of DataReader for StreamSocket under concurrency:: create_task?
Thank you very much for your help in advance!

STM32F4 HAL ADC DMA Transfer Error

I'm using an STM32F405OG for a project and one of the necessary functions is monitoring 3 analog channels at ~1 Hz. My desired implementation is starting an ADC DMA read of all 3 channels in Scan mode and retrieving the results at a later time after the DMA complete interrupt has occurred.
I'm using ADC1 and have tried both DMA channels 0 and 4, both with the same result: HAL_ADC_ErrorCallback() is invoked after the first call to HAL_ADC_Start_DMA(). At this point, the ADC handle is in an error state (HAL_ADC_STATE_ERROR_DMA) with the error code 0x04 (HAL_ADC_ERROR_DMA). Checking the linked DMA handle yields a DMA error code of HAL_DMA_ERROR_NO_XFER, meaning "Abort requested with no Xfer ongoing."
I'm totally lost as to what's causing this - my code should be consistent with examples and the "how to use this module" comments at the top of stm32f4xx_hal_adc.c. I've attached my code below.
ADC_HandleTypeDef ADC_hADC =
{
.Instance = ADC1,
.Init =
{
.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8,
.Resolution = ADC_RESOLUTION_12B,
.EOCSelection = ADC_EOC_SEQ_CONV, // EOC at end of sequence of channel conversions
.ScanConvMode = ENABLE,
.ContinuousConvMode = DISABLE,
.DiscontinuousConvMode = DISABLE,
.NbrOfDiscConversion = 0U,
.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE,
.ExternalTrigConv = ADC_SOFTWARE_START,
.DataAlign = ADC_DATAALIGN_RIGHT,
.NbrOfConversion = _NUM_ADC_CONV,
.DMAContinuousRequests = DISABLE
}
};
DMA_HandleTypeDef _hDmaAdc =
{
.Instance = DMA2_Stream0,
.Init =
{
.Channel = DMA_CHANNEL_0,
.Direction = DMA_PERIPH_TO_MEMORY,
.PeriphInc = DMA_PINC_DISABLE,
.MemInc = DMA_MINC_ENABLE,
.PeriphDataAlignment = DMA_PDATAALIGN_WORD,
.MemDataAlignment = DMA_MDATAALIGN_WORD,
.Mode = DMA_NORMAL,
.Priority = DMA_PRIORITY_HIGH,
.FIFOMode = DMA_FIFOMODE_DISABLE,
.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL,
.MemBurst = DMA_MBURST_SINGLE,
.PeriphBurst = DMA_PBURST_SINGLE
}
};
void HAL_ADC_MspInit(ADC_HandleTypeDef *h)
{
if (!h)
{
return;
}
else if (h->Instance == ADC1)
{
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_DMA2_CLK_ENABLE();
HAL_DMA_Init(&_hDmaAdc);
__HAL_LINKDMA(h, DMA_Handle, _hDmaAdc);
HAL_NVIC_SetPriority(ADC_IRQn, IT_PRIO_ADC, 0);
HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, IT_PRIO_ADC, 0);
HAL_NVIC_EnableIRQ(ADC_IRQn);
HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
}
}
uint32_t _meas[3];
ADC_ChannelConfTypeDef _chanCfg[3] =
{
// VIN_MON
{
.Channel = ADC_CHANNEL_1,
},
// VDD_MON
{
.Channel = ADC_CHANNEL_8,
},
// VDD2_MON
{
.Channel = ADC_CHANNEL_2,
}
};
Bool ADC_Init(void)
{
ADC_DeInit();
memset(_meas, 0, sizeof(_meas));
Bool status = (HAL_ADC_Init(&ADC_hADC) == HAL_OK);
if (status)
{
// Configure each ADC channel
for (uint32_t i = 0U; i < NELEM(_chanCfg); i++)
{
_chanCfg[i].Rank = (i + 1U);
_chanCfg[i].SamplingTime = ADC_SAMPLETIME_480CYCLES;
_chanCfg[i].Offset = 0U;
if (HAL_ADC_ConfigChannel(&ADC_hADC, &_chanCfg[i]) != HAL_OK)
{
status = FALSE;
break;
}
}
_state = ADC_STATE_READY;
}
if (!status)
{
ADC_DeInit();
}
return status;
}
Bool ADC_StartRead(void)
{
Bool status = TRUE;
status = (HAL_ADC_Start_DMA(&ADC_hADC, &_meas[0], 3) == HAL_OK);
return status;
}
After slowing down the conversions via the ClockPrescaler init structure field, increasing the number of ADC cycles, and calling HAL_ADC_Stop_DMA() (per file header comment in stm32f4xx_hal_adc.c), everything is working.
Note that calling HAL_ADC_Stop_DMA() in the DMA Transfer Complete ISR caused the aforementioned error conditions as well, so calls to that function will have to be made sometime after the DMAXferCplt ISR is invoked.

How to decrypt data that is encrypted by OpenSSL with RSA_PKCS1_OAEP_PADDING using NCryptDecrypt?

I have difficulty to decrypt data being encrypted using OpenSSL and RSA_PKCS1_OAEP_PADDING padding option.
What I am doing is:
BCRYPT_ALG_HANDLE hCryptAlg = NULL;
BCRYPT_OAEP_PADDING_INFO paddingInfo = { 0 };
DWORD cbDecryptedMessage;
BYTE* pbDecryptedMessage = NULL;
paddingInfo.pszAlgId = BCRYPT_SHA1_ALGORITHM;
// Open an algorithm handle.
BCryptOpenAlgorithmProvider(&hCryptAlg, BCRYPT_RSA_ALGORITHM, NULL, 0);
// Calculate the required buffer
NCryptDecrypt(m_hKeyContextFull, (LPBYTE)pEncrypted, encryptedLenInBytes, &paddingInfo, NULL, cbDecryptedMessage, &outputDataLen, NCRYPT_PAD_OAEP_FLAG | NCRYPT_SILENT_FLAG);
// After required buffer is allocated...
NCryptDecrypt(m_hKeyContextFull, (LPBYTE)pEncrypted, encryptedLenInBytes, &paddingInfo, pbDecryptedMessage, cbDecryptedMessage, &outputDataLen, NCRYPT_PAD_OAEP_FLAG | NCRYPT_SILENT_FLAG);
It fails with NTE_INVALID_PARAMETER (0x80090027). I tried different flags but none of them works.
Note: m_hKeyContextFull has already been retrieved using CryptAcquireCertificatePrivateKey function call:
m_hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE, m_storeName.c_str());
m_pCertWithKeys = CertFindCertificateInStore(m_hSystemStore, SupportedEncodings, 0, CERT_FIND_SUBJECT_STR, m_certName.c_str(), NULL);
// Obtain the private key from the certificate.
DWORD m_KeyContextSpec = 0;
HCRYPTPROV_OR_NCRYPT_KEY_HANDLE m_hKeyContextFull;
CryptAcquireCertificatePrivateKey(m_pCertWithKeys, CRYPT_ACQUIRE_SILENT_FLAG | CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG, NULL, &m_hKeyContextFull, &m_KeyContextSpec, &m_KeyContextMustBeReleased);
Note: All error checkings have been removed from code for readability.
Is there any idea what am I doing wrong?
Thanks.
I'd start by making paddingInfo valid:
BCRYPT_OAEP_PADDING_INFO paddingInfo;
paddingInfo.pszAlgId = BCRYPT_SHA1_ALGORITHM;
paddingInfo.pbLabel = NULL;
paddingInfo.cbLabel = 0;
The current standard has the empty string label (see RFC 3447).
Moreover, as to pbDecryptedMessage:
If this parameter is NULL, this function will calculate the size needed for the decrypted data and return the size in the location pointed to by the pcbResult parameter.
So it won't decrypt anyway. You need to allocate the buffer first to the right size; a lot of Windows API's work that way: first call it with output buffer NULL and you get back the needed size somehow, in this case in outputDataLen (which you should declare!). Then allocate the outbufbuffer to that size, and call the function again with the fresh buffer, and the correct length in cbDecryptedMessage. After use, free the buffer again of course. But your comment claims to have done so?
Another suspicious fact: You use NCryptDecrypt so the first argument, should
be a hKey of the right type, while m_hKeyContextFull does not seem to have that type. CryptAcquireCertificatePrivateKey will get you an old style key handle.
You cannot mix these different Windows CryptoAPI's.
Maybe look at the NcryptImportKey function, to transfer it.
I'm not familiar with the NCrypt series of interfaces, but we implemented something similar recently in a library using the BCrypt series of interfaces. Here is the function in question, which can be seen in greater context here.
In our case prvblbtyp is LEGACY_RSAPRIVATE_BLOB and prvblbbuf and prvblbblen are described here.
static
int
asymmetric_decrypt(
const wchar_t * const prvblbtyp,
const void * const prvblbbuf, const size_t prvblblen,
const void * const ctbuf, const size_t ctlen,
void ** const ptbuf, size_t * const ptlen)
{
BCRYPT_ALG_HANDLE halg;
int res;
res = INT_MIN;
if (BCryptOpenAlgorithmProvider(
&halg, BCRYPT_RSA_ALGORITHM, NULL, 0) == STATUS_SUCCESS) {
BCRYPT_KEY_HANDLE hkey;
if (BCryptImportKeyPair(
halg,
NULL, prvblbtyp, &hkey,
(void *)prvblbbuf, prvblblen,
0) == STATUS_SUCCESS) {
BCRYPT_OAEP_PADDING_INFO inf;
ULONG len;
inf.pszAlgId = BCRYPT_SHA1_ALGORITHM;
inf.pbLabel = NULL;
inf.cbLabel = 0;
/*
* decrypt first with a NULL output buffer.
* this returns the size necessary for the buffer.
*/
if (BCryptDecrypt(
hkey,
(void *)ctbuf, ctlen,
&inf,
NULL, 0,
NULL, 0, &len,
BCRYPT_PAD_OAEP) == STATUS_SUCCESS) {
void * buf;
/*
* allocate the required buffer
* and decrypt again
*/
res = -ENOMEM;
buf = malloc(len);
if (buf) {
res = INT_MIN;
if (BCryptDecrypt(
hkey,
(void *)ctbuf, ctlen,
&inf,
NULL, 0,
buf, len, &len,
BCRYPT_PAD_OAEP) == STATUS_SUCCESS) {
*ptbuf = buf;
*ptlen = len;
res = 0;
} else {
free(buf);
}
}
}
BCryptDestroyKey(hkey);
}
BCryptCloseAlgorithmProvider(halg, 0);
}
return res;
}

LwIP Netconn API + FreeRTOS TCP Client Buffer Issue

I've been trying to modify the tcp server example with LwIP in STM32F4DISCOVERY board. I have to write a sender which does not necessarily have to reply server responses. It can send data with 100 ms frequency, for example.
Firstly, the example of TCP server is like this:
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL) {
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, DEST_PORT);
if (err == ERR_OK) {
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1) {
/* Grab new connection. */
newconn = netconn_accept(conn);
/* Process the new connection. */
if (newconn) {
struct netbuf *buf;
void *data;
u16_t len;
while ((buf = netconn_recv(newconn)) != NULL) {
do {
netbuf_data(buf, &data, &len);
//Incoming package
.....
//Check for data
if (DATA IS CORRECT)
{
//Reply
data = "OK";
len = 2;
netconn_write(newconn, data, len, NETCONN_COPY);
}
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
} else {
printf(" can not bind TCP netconn");
}
} else {
printf("can not create TCP netconn");
}
}
I modified this code to obtain a client version, this is what I've got so far:
static void tcpecho_thread(void *arg)
{
struct netconn *xNetConn = NULL;
struct ip_addr local_ip;
struct ip_addr remote_ip;
int rc1, rc2;
struct netbuf *Gonderilen_Buf = NULL;
struct netbuf *gonderilen_buf = NULL;
void *b_data;
u16_t b_len;
IP4_ADDR( &local_ip, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3 );
IP4_ADDR( &remote_ip, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
xNetConn = netconn_new ( NETCONN_TCP );
rc1 = netconn_bind ( xNetConn, &local_ip, DEST_PORT );
rc2 = netconn_connect ( xNetConn, &remote_ip, DEST_PORT );
b_data = "+24C"; // Data to be send
b_len = sizeof ( b_data );
while(1)
{
if ( rc1 == ERR_OK )
{
// If button pressed, send data "+24C" to server
if (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET)
{
Buf = netbuf_new();
netbuf_alloc(Buf, 4); // 4 bytes of buffer
Buf->p->payload = "+24C";
Buf->p->len = 4;
netconn_write(xNetConn, Buf->p->payload, b_len, NETCONN_COPY);
vTaskDelay(100); // To see the result easily in Comm Operator
netbuf_delete(Buf);
}
}
if ( rc1 != ERR_OK || rc2 != ERR_OK )
{
netconn_delete ( xNetConn );
}
}
}
While the writing operation works, netconn_write sends what's on its buffer. It doesnt care whether b_data is NULL or not. I've tested it by adding the line b_data = NULL;
So the resulting output in Comm Operator is like this:
Rec:(02:47:27)+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C
However, I want it to work like this:
Rec:(02:47:22)+24C
Rec:(02:47:27)+24C
Rec:(02:57:12)+24C
Rec:(02:58:41)+24C
The desired write operation happens when I wait for around 8 seconds before I push the button again.
Since netconn_write function does not allow writing to a buffer, I'm not able to clear it. And netconn_send is only allowed for UDP connections.
I need some guidance to understand the problem and to generate a solution for it.
Any help will be greately appreciated.
It's just a matter of printing the result in the correct way.
You can try to add this part of code before writing in the netbuf data structure:
char buffer[20];
sprintf(buffer,"24+ \n");
Buf->p->payload = "+24C";
I see one or two problems in your code, depending on what you want it exactly to do. First of all, you're not sending b_data at all, but a constant string:
b_data = "+24C"; // Data to be send
and then
Buf->p->payload = "+24C";
Buf->p->len = 4;
netconn_write(xNetConn, Buf->p->payload, b_len, NETCONN_COPY);
b_data is not anywhere mentioned there. What is sent is the payload. Try Buf->p->payload = b_data; if it's what you want to achieve.
Second, if you want the +24C text to be sent only once when you push the button, you'll have to have a loop to wait for the button to open again before continuing the loop, or it will send +24C continuously until you stop pushing the button. Something in this direction:
while (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET) {
vTaskDelay(1);
}

How to control socket on peer[TCP Hole Punching]

I have a server-client program that use TCP connection to communicate. More than one client can connect to the server at the same time. I want to implement the tcp hole punching on this system.
On the client side, It calls to the public server to look up the public ip,port of my server. Then connect to it.
But on the server side it has to open a port to connect to the public server, and it also has to accept the client connection request on this same port.
What I'm going to do is opening a socket and bind to port X, then connect to the public server, then change this socket to listening state to accept the incoming connection for some period, then start connection to the public server again, over and over.
Is this the right approach ?
EDIT: I have another idea. It is to open a new port and connect to the public server. The main server port is left listening for incoming connection as usual. When the client want to connect, the public server will tell my server via the new port. It will stop the main port from listen the incoming connection, instead, It will connect to the client to do the hole punching. Then It connect to the public server, which will forward the server public ip address to the client, and goes back to listen for incoming connection as usual. The client will then use this address to connect to the server which TCP hole already opened.
Better have two socket and maintain separate the conection between server and client.
m_nServerTCPSocket- used to connect and listner socket with server
m_nPeerPrivateTCPSocket- to connect with peer (public address)
m_nPeerPublicTCPSocket- to connect with peer (private address if other peer is in the same network)
m_nListeningTCPSocket - used to listener socket for peer here u need to accept connection from peer.
m_nConnectedPeerTCPSocket-> you get this socket once you connected with other peer.
while(end_client)
{
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
FD_ZERO(&fdExcept);
if (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket()>0)
{
FD_SET (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdRead);
FD_SET (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdExcept);
}
if (pControlMgr->GetConnectionMgr()->GetServerTCPSocket()>0)
{
FD_SET (pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),&fdRead);
FD_SET (pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),&fdExcept);
}
if (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket()>0)
{
FD_SET (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),&fdRead);
FD_SET (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),&fdExcept);
}
timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
nSelectRetVal = select(NULL,&fdRead,NULL,&fdExcept,&tv);
if (nSelectRetVal>0)
{
int nRecvRetVal = 0;
/* TCP Server Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(), &fdRead ))
{
try
{
pRecvBuffer = new char[TCP_RECV_SIZE];
nRecvRetVal = recv(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),
pRecvBuffer,TCP_RECV_SIZE,
0);
int n = WSAGetLastError();
if (nRecvRetVal>0)
{
int nPeerNameRetVal = getpeername(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),(sockaddr*)&addrRemotePeer,&nSockAddrLen);
if ( pControlMgr->HandlePacket(pRecvBuffer,addrRemotePeer)== -1 )
{
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
return 0 ;
}
}
}
}
catch (...)
{
if ( NULL != pRecvBuffer )
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
}
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
} /* TCP Server Socket handling */
int n;
/* TCP Exception Server Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(), &fdExcept ))
{
/*FD_CLR(pControlMgr->GetConnectionMgr().GetServerTCPSocket (),&fdRead);
FD_CLR(pControlMgr->GetConnectionMgr().GetServerTCPSocket (),&fdExcept);*/
n = WSAGetLastError();
//return 0;
}
if (FD_ISSET(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdRead))
{
sockaddr_in addrConnectedPeer;
int nAddrLen =sizeof(addrConnectedPeer) ;
int nConnectedSock = accept( pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),
(sockaddr*)&addrConnectedPeer,
&nAddrLen);
int n1 = WSAGetLastError();
if (nConnectedSock>0)
{
pControlMgr->GetConnectionMgr()->SetConnectedTCPSocket(nConnectedSock);
int n = pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket();
continue;
}
}
/* TCP Exception Listening Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(), &fdExcept ))
{
FD_CLR(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket (),&fdRead);
FD_CLR(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket (),&fdExcept);
//return 0;
} /* TCP Exception Listening Socket handling */
/* Connected Peer TCP Read Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(), &fdRead ))
{
try
{
pRecvBuffer = new char[TCP_RECV_SIZE];
nRecvRetVal = recv (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),
pRecvBuffer,TCP_RECV_SIZE,
0);
if (nRecvRetVal>0)
{
int nPeerNameRetVal = getpeername(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),(sockaddr*)&addrRemotePeer,&nSockAddrLen);
if ( pControlMgr->HandlePacket(pRecvBuffer,addrRemotePeer)== -1 )
{
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
return 0 ;
}
}
}
}
catch (...)
{
if ( NULL != pRecvBuffer )
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
}
//FD_CLR(pControlMgr->GetConnectionMgr().GetConnectedTCPSocket(),&fdRead);
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
} /* Peer TCP Read Socket handling */
/* TCP Exception Connected Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(), &fdExcept ))
{
/*FD_CLR(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket (),&fdRead);
FD_CLR(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket (),&fdExcept);
return 0;*/
n = WSAGetLastError();
}
logic to create sockets
int CConnectionMgr::CreateSocket(const int nSockType)
{
//TODO: Add code here
if (InitWinSock() == -1)
{
return -1;
}
SetLocalIPAddress();
m_nListeningTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if ( GetListeningTCPSocket() <0 )
return -1;
if (BindSocket(GetListeningTCPSocket())<0)
return -1;
int nListenRet = listen(GetListeningTCPSocket(),SOMAXCONN);
if (nListenRet!=0)
{
return -1;
}
m_nPeerPrivateTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if (GetPeerPrivateTCPSocket()<0)
return -1;
if (BindSocket(GetPeerPrivateTCPSocket())<0)
return -1;
m_nPeerPublicTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if ( GetPeerPublicTCPSocket()<0)
return -1;
if (BindSocket(GetPeerPublicTCPSocket())<0)
return -1;
m_nServerTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if (GetServerTCPSocket()<0)
return -1;
if (BindSocket(GetServerTCPSocket())<0)
return -1;
return 1;
}

Resources