gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Wifi - Ad-Hoc (Sending Structures)

#168670 - DarkShadow44 - Thu May 14, 2009 7:30 pm

Hello!
I'm trying to send a struct with the wifi functions, but I can't receive them.
Do you know what's wrong with this code:

Code:

#include <nds.h>
#include <stdio.h>
#include <dswifi9.h>
#include <string.h>




typedef struct
{
   int a;
   int b;
}structure;



structure str1;





void Handler(int packetID, int readlength)
{
   structure str2;
   
   str2.a=0;
   str2.b=0;

   structure* strPtr2=&str2;



   static int bytesRead;

   bytesRead = Wifi_RxRawReadPacket(packetID, readlength, (unsigned short*)strPtr2);


   {
      iprintf("\x1b[1;0H\n Bytes: %d!\nRcv:%d , %d ", bytesRead,str2.a, str2.b); //Test Received Structure
   }
}

int main(void)
{
   consoleDemoInit();

   Wifi_InitDefault(false);
   Wifi_SetPromiscuousMode(1);
   Wifi_EnableWifi();
   Wifi_RawSetPacketHandler(Handler);
   Wifi_SetChannel(10);

   iprintf("Press A to start");
   
   while(1)
   {
      scanKeys();
      int pressed = keysDown();
      if(pressed & KEY_A)
         break;
   }

   iprintf("\x1b[0;0H                      ");

   int send = 0;
   int f;

   str1.a=99;
   str1.b=34;

   structure*strPtr1=&str1;


   while(1)
   {
      iprintf("\x1b[0;0HSending %d", send);

      if(Wifi_RawTxFrameAdHoc(sizeof(structure), 0x0014, (unsigned short*)strPtr1) != 0)
      {
         iprintf("\n\nError calling RawTxFrame\n\n");
      }

      send++;
      for(f = 0 ; f < 6 ; f++)
         swiWaitForVBlank();
   }

   return 0;
}
 


The function RawTxFrameAdHoc is from here: http://forum.gbadev.org/viewtopic.php?t=16394&postdays=0&postorder=asc&start=15 (I renamed the function RawTxFrameAdHoc)


Please Help !!!

#168672 - Pete_Lockwood - Fri May 15, 2009 12:08 am

If you have wireless routers around you, there's probably a good chance that code will crash because you'll receive frames from the routers that will be picked up by the Handler function and read into the tiny structure even though the frame is too big (i.e. memory corruption).

Are you able to get the actual test app that I posted working? i.e. is the issue only with your changed copy of the code? Or can't you get my code working either?
_________________
It's not an illusion, it just looks like one.

#168681 - DarkShadow44 - Fri May 15, 2009 4:04 pm

I have the Example code from http://forum.gbadev.org/viewtopic.php?t=16394&postdays=0&postorder=asc&start=15 .

I've tried this application and it worked...
It saves the data in the arry
Code:
unsigned short data [4096]



Where can I download your app ?

#168687 - Pete_Lockwood - Fri May 15, 2009 5:10 pm

Sorry, I meant the test app in that post. If that worked for you, as it certainly should, perhaps the only issue is that you're using a small buffer to receive data into. You could try this change to the handler:

Code:

void Handler(int packetID, int readlength)
{
   static char data[4096];
   static int bytesRead;
   structure *str2;
   
   bytesRead = Wifi_RxRawReadPacket(packetID, readlength, (unsigned short *)data);
   {
      str2 = (structure *)data;
      iprintf("\x1b[1;0H\n Bytes: %d!\nRcv:%d , %d ", bytesRead,str2->a, str2->b); //Test Received Structure
   }
}


Note, I haven't even tried compiling this so syntax might be slightly off but this should give you an idea anyway.

This code would still treat every packet received as if it was a "structure" packet, including packets from Wifi routers so it would still need to be improved to filter out garbage, perhaps by putting a checksum into the structure and validating that when you receive the data.
_________________
It's not an illusion, it just looks like one.

#168690 - DarkShadow44 - Fri May 15, 2009 6:43 pm

Could it also be the wrong size to send the structure ?
Or wrong casting ?

And how can I create a Checksum ?

#168693 - elhobbs - Fri May 15, 2009 9:31 pm

I am not sure if you read the whole thread about how this code works, but you may want to before you invest a lot of time making this work. It sends all of the packets as if they are AP management packets (the ds receives AP management packets by default and none of the exisiting wifi libs can set the filter to receive non-management packets reliably).

This can have a very detrimental effect on wifi APs and systems connected to said APs that are in range of this transmission. essentially no one is going to want to run your code if you are doing this. if it is just for yourself and you do not need to worry about breaking wifi for those around you then maybe it is not an issue.

#168707 - DarkShadow44 - Sat May 16, 2009 11:25 am

But does it work with a checksum, and how can I use checksums for wifi ?

#168708 - elhobbs - Sat May 16, 2009 2:38 pm

no, it will not help your code work. he is suggesting a checksum to differentiate all of the legitimate AP management frames from the hack ones that this code is sending out.

most likely your code is dying when it receives the first packet because your buffer is too small. you are assuming all of the packets you receive are yours and of a fixed size.

#168711 - DarkShadow44 - Sat May 16, 2009 5:02 pm

But how could I check if it is my packet ?

It also hangs, if I use 2 DS for sending and receiving...
Sometimes it don't hangs, it displays only wrong values.

#168715 - elhobbs - Sat May 16, 2009 7:09 pm

perhaps you should try your favorite search engine and search for "checksum"... then come back when you have a specific question.

you may want to take a look at "buffer overflow" too...