I'm not an expert in C and have some understanding problems regarding char conversion. I've written some code which is working just fine, but I don't like it that much...
I was hoping one you guys can come up with an better, more optimized version. Also I'm getting an output like this: NETAPP EVENT] IP Acquired: 192.168.0__.111 (_ = space). How can I get rid of the spaces, when there is only one digit?
Thanks
Embedded working coder to print an PIv4 address to the serial console:
...
//Print IP in UART
sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, &dhcpIsOn, &len,(unsigned char *)&ipV4);
const unsigned char ip_c_3 = SL_IPV4_BYTE(ipV4.ipV4, 3);
char ip_3[3];
sprintf(ip_3,"%ld", (int)ip_c_3);
const unsigned char ip_c_2 = SL_IPV4_BYTE(ipV4.ipV4, 2);
char ip_2[3];
sprintf(ip_2,"%ld", (int)ip_c_2);
const unsigned char ip_c_1 = SL_IPV4_BYTE(ipV4.ipV4, 1);
char ip_1[3];
sprintf(ip_1,"%ld", (int)ip_c_1);
const unsigned char ip_c_0 = SL_IPV4_BYTE(ipV4.ipV4, 0);
char ip_0[3];
sprintf(ip_0,"%ld", (int)ip_c_0);
UART_write(uart,"[NETAPP EVENT] IP Acquired: ",sizeof("[NETAPP EVENT] IP Acquired: "));
UART_write(uart,ip_3, sizeof(ip_3));
UART_write(uart,".",1);
UART_write(uart,ip_2,sizeof(ip_2));
UART_write(uart,".",1);
UART_write(uart,ip_1,sizeof(ip_1));
UART_write(uart,".",1);
UART_write(uart,ip_0,sizeof(ip_0));
UART_write(uart,"\n",2);
...
UART_write:
/*!
* @brief Function that writes data to a UART
*
* This function initiates an operation to write data to a UART controller.
*
* In UART_MODE_BLOCKING, UART_write will block task execution until all
* the data in buffer has been written.
*
* In UART_MODE_CALLBACK, UART_write does not block task execution an calls a
* callback function specified by writeCallback.
*
* @param handle A UART_Handle
*
* @param buffer A pointer to buffer containing data to be written
*
* @param size The number of bytes in buffer that should be written
* onto the UART.
*
* @return Returns the number of bytes that have been written to the UART,
* UART_ERROR on an error.
*/
extern int UART_write(UART_Handle handle, const void *buffer, size_t size);
sl_NetCfgGet:
/*!
\brief Internal function for getting network configurations
\return On success, zero is returned. On error, -1 is
returned
\param[in] ConfigId configuration id
\param[out] pConfigOpt Get configurations option
\param[out] pConfigLen The length of the allocated memory as input, when the
function complete, the value of this parameter would be
the len that actually read from the device.\n
If the device return length that is longer from the input
value, the function will cut the end of the returned structure
and will return ESMALLBUF
\param[out] pValues - get configurations values
\sa
\note
\warning
\par
\sample code
SL_IPV4_AP_P2P_GO_GET_INFO:
Get static IP address for AP or P2P go.
_u8 len = sizeof(SlNetCfgIpV4Args_t);
_u8 dhcpIsOn = 0; // this flag is meaningless on AP/P2P go.
SlNetCfgIpV4Args_t ipV4 = {0};
sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO,&dhcpIsOn,&len,(_u8 *)&ipV4);
printf("IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d DNS %d.%d.%d.%d\n",
SL_IPV4_BYTE(ipV4.ipV4,3),SL_IPV4_BYTE(ipV4.ipV4,2),SL_IPV4_BYTE(ipV4.ipV4,1),SL_IPV4_BYTE(ipV4.ipV4,0),
SL_IPV4_BYTE(ipV4.ipV4Mask,3),SL_IPV4_BYTE(ipV4.ipV4Mask,2),SL_IPV4_BYTE(ipV4.ipV4Mask,1),SL_IPV4_BYTE(ipV4.ipV4Mask,0),
SL_IPV4_BYTE(ipV4.ipV4Gateway,3),SL_IPV4_BYTE(ipV4.ipV4Gateway,2),SL_IPV4_BYTE(ipV4.ipV4Gateway,1),SL_IPV4_BYTE(ipV4.ipV4Gateway,0),
SL_IPV4_BYTE(ipV4.ipV4DnsServer,3),SL_IPV4_BYTE(ipV4.ipV4DnsServer,2),SL_IPV4_BYTE(ipV4.ipV4DnsServer,1),SL_IPV4_BYTE(ipV4.ipV4DnsServer,0));
\endcode
*/
#if _SL_INCLUDE_FUNC(sl_NetCfgGet)
_i32 sl_NetCfgGet(_u8 ConfigId ,_u8 *pConfigOpt, _u8 *pConfigLen, _u8 *pValues);
#endif
You can reduce all this:
const unsigned char ip_c_3 = SL_IPV4_BYTE(ipV4.ipV4, 3);
char ip_3[3];
sprintf(ip_3,"%ld", (int)ip_c_3);
const unsigned char ip_c_2 = SL_IPV4_BYTE(ipV4.ipV4, 2);
char ip_2[3];
sprintf(ip_2,"%ld", (int)ip_c_2);
const unsigned char ip_c_1 = SL_IPV4_BYTE(ipV4.ipV4, 1);
char ip_1[3];
sprintf(ip_1,"%ld", (int)ip_c_1);
const unsigned char ip_c_0 = SL_IPV4_BYTE(ipV4.ipV4, 0);
char ip_0[3];
sprintf(ip_0,"%ld", (int)ip_c_0);
UART_write(uart,"[NETAPP EVENT] IP Acquired: ",sizeof("[NETAPP EVENT] IP Acquired: "));
UART_write(uart,ip_3, sizeof(ip_3));
UART_write(uart,".",1);
UART_write(uart,ip_2,sizeof(ip_2));
UART_write(uart,".",1);
UART_write(uart,ip_1,sizeof(ip_1));
UART_write(uart,".",1);
UART_write(uart,ip_0,sizeof(ip_0));
UART_write(uart,"\n",2);
to just:
const char *msg = "[NETAPP EVENT] IP Acquired";
const unsigned int ip_c_3 = SL_IPV4_BYTE(ipV4.ipV4, 3);
const unsigned int ip_c_2 = SL_IPV4_BYTE(ipV4.ipV4, 2);
const unsigned int ip_c_1 = SL_IPV4_BYTE(ipV4.ipV4, 1);
const unsigned int ip_c_0 = SL_IPV4_BYTE(ipV4.ipV4, 0);
char ip[256];
sprintf(ip, "%s: %u.%u.%u.%u\n", msg, ip_c_3, ip_c_2, ip_c_1, ip_c_0);
UART_write(uart, ip, strlen(ip));
Note: this also fixes various bugs in the original code, including the problem where unwanted spaces were being introduced into the output.