naresh k


I am working on a Android java client to send OTA firmware to a RTL8195 based board. The board runs an OTA firmware accept program that listens to a client on port 5000 and then expects the client to send two things

1. OTA description: this is a 3 value integer array having the following data
[0] the checksum of firmware
[1] the value 0
[2] the size of ota firmware
2. firmware file itself

Here is a snippet of the OTA.cpp firmware opening a port and listening to the the client


server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_socket < 0) {
OTA_PRINTF("Fail to create socket\r\n");

memset(&localHost, 0, sizeof(localHost));
localHost.sin_family = AF_INET;
localHost.sin_port = htons(port);
localHost.sin_addr.s_addr = INADDR_ANY;

if (lwip_bind(server_socket, (struct sockaddr *)&localHost, sizeof(localHost)) < 0) {
OTA_PRINTF("Bind fail\r\n");

if (lwip_listen(server_socket , 1) < 0) {
OTA_PRINTF("Listen fail\r\n");

OTA_PRINTF("Wait for client\r\n");
n = (int) sizeof( client_addr );
memset(&client_addr, 0, sizeof(client_addr));
client_socket = lwip_accept(server_socket, (struct sockaddr *) &client_addr, (socklen_t *)&n);
OTA_PRINTF("Client connected. IP:%s port:%d\r\n\r\n", inet_ntoa(client_addr.sin_addr.s_addr), ntohs(client_addr.sin_port));

lwip_setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &socket_timeout, sizeof(socket_timeout));

OTA_PRINTF("Read OTA info...\r\n");
read_bytes = read(client_socket, file_info, sizeof(file_info));
if (read_bytes < 0) {
OTA_PRINTF("Fail to read OTA info\r\n");

for(int i=0; i<3; i++){
OTA_PRINTF("%i \n" , file_info[i]);

if (file_info[2] == 0) {
OTA_PRINTF("OTA image len is 0\r\n");

ota_len = file_info[2];
ota_blk_size = ((ota_len - 1) / 4096) + 1;
for (i = 0; i < ota_blk_size; i++) {
flash_erase_sector(&flash, ota_addr + i * 4096);

Now, my android java client is expected to first send the three index integer array containing the three values as mentioned before. The java code is below

private boolean sendOTAToDevice(AmebaDevice device, byte[] fwBytes, int fwLength, int[] otaDesc) throws IOException{
    Log.d(TAG, "Trying connection to :: " + device.getDeviceIP() + ":" + device.getDevicePort());
    Socket deviceSocket = new Socket(device.getDeviceIP(), device.getDevicePort());

    OutputStream os = deviceSocket.getOutputStream();
    ObjectOutputStream stream = new ObjectOutputStream(os);

    fwBytes = Arrays.copyOf(fwBytes, fwLength);
    Log.i(TAG, "Sending OTA Decsription");
    os.write(integersToBytes(otaDesc), 0, otaDesc.length);

    //commented out for now as i can see garbage value being posted even with just a socket opening
    //Log.i(TAG, "Sending OTA image");
    //os.write(fwBytes, 0, fwLength);
    return true;

Here is my function of integerToBytes():

private byte[] integersToBytes(int[] values) throws IOException{
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    DataOutputStream dataStream = new DataOutputStream(byteOutputStream);
    byte[] result = new byte[values.length];
    for(int i=0; i<values.length; i++){

    byte[] b =  byteOutputStream.toByteArray();
    return b;    


The surprising thing i am seeing is, even when i just say Socket = new Socket(ip, port), I am seeing that the device is getting some garbage value (though this value is consistent every time) and the device fails to acknowledge the ota description (the 3 index integer) value.

In the code above i have opened up a OutputStream and also tried closing it properly else where in the activity lifecycle to manage the stream correctly. I also tried commenting out all the stream code and just tried creating a Socket object but even when the only thing the code is doing is open a socket, i can see the the RTL8195 device is getting the garbage value.

Could anyone help me understand what is going on, Any pointers would be appreciated.

naresh k 編輯了提問