Communication between CrankApp in Android and C(Linux)

All things Storyboard for the Android platform.

Moderator: jamiev

Communication between CrankApp in Android and C(Linux)

Postby atinir » Wed Mar 22, 2017 7:15 am

Hi,

I want to deploy my Crank app (gapp file) to Android device. (This part I manged to do it by exporting the Crank app as apk file and installing in Android device)
I also have a backend program in C which controls the Crank app. This backend program in C I want to run it in Linux machine. The Linux machine can be connected to Android device either with cable or wifi connection.

Can the apk file as it is installed in Android, receive and send events to C program? Is there a need to install extra files in Android? Also If I have Crank app in one machine, and C program in another machine what protocol should I use for communication?

Thank you!

All the best,
Atinir
atinir
 
Posts: 25
Joined: Thu Apr 21, 2016 9:20 am

Re: Communication between CrankApp in Android and C(Linux)

Postby nschultz » Wed Mar 22, 2017 8:01 am

Hello Atinir,

This is certainly possible to accomplish, generally we would suggest using a socket connection if you are looking to communicate between android and a separate linux machine. To do this you will have to compile a lua module that will take care of the socket connection. Take a look at this forum post to get an idea of what's involved with building Lua modules. There is a link to the lua sockets source so you shouldn't have to write anything, just compile against the libraries shipped in the storyboard android runtime.

http://forums.cranksoftware.com/viewtopic.php?f=6&t=503

When you have the lua module compiled you will need to include it in your project, to do this see the section in our documentation about including extra libraries for Android. http://www.cranksoftware.com/cranksoftware/v5.0.0/docs/ch23s02.html#d0e5683

The last thing you will need to do is to added the appropriate permission to the Android manifest, this can be done during the export stage. In your case you will want "android.permission.INTERNET", the full list of permissions can be found here. https://developer.android.com/reference/android/Manifest.permission.html

If you wanted more information about writing custom modules then please take a look at this post which provides a sample to start with.

http://www.cranksoftware.com/forums/viewtopic.php?f=6&t=449&p=1219&hilit=lua+module#p1219
nschultz
 
Posts: 16
Joined: Fri Jul 17, 2015 1:51 pm

Re: Communication between CrankApp in Android and C(Linux)

Postby atinir » Thu Mar 23, 2017 7:09 am

Hi,

Thank you so much for all this information. So as I understood, Lua module will take care for the socket connection on the client side.
What about the C backend, when I send and receive events via greio, should I change here something to serialize or deserialize the data in terms of the socket connection? The same goes for Lua, should now I program in terms of socket to receive or send data?

Do you have some snippet code that explain these? Just to have an idea, how should I program when there is socket connection.

Thank you so much!

All the best,
Atinir
atinir
 
Posts: 25
Joined: Thu Apr 21, 2016 9:20 am

Re: Communication between CrankApp in Android and C(Linux)

Postby nschultz » Thu Mar 23, 2017 8:15 am

Hello Atinir,

There are two ways which you could go about this, if you are just using tcp sockets you could define your own event protocol and simply send them over the network. This would remove the need for the greio connection and event structure. However if the code you are writing is intended to be used locally (ipc) or over tcp then you might want to take a look at the Storyboard IO TCP/IP relay in this post. This will allow you to send Storyboard IO events over a tcp connection and allows your UI to live on different devices than your backend code.

http://forums.cranksoftware.com/viewtopic.php?f=7&t=464

Thank you

Nik
nschultz
 
Posts: 16
Joined: Fri Jul 17, 2015 1:51 pm

Re: Communication between CrankApp in Android and C(Linux)

Postby atinir » Sat Mar 25, 2017 11:06 pm

Hi Nik,

Thank you again for the useful information.

Yes the code will be used over TCP socket connection. I did look at the example for TCP/IP relay. So, if I use the example from the post, should I use Luasocket module that in the beginning of my post you suggested to use, or the LUA module sbio_tcprelay.so included in the example is enough (it has the same functionality?)

Also when analyzing the code, I realized that for the client there is a channel that only reads handle = gre_io_open(channel_name, GRE_IO_TYPE_RDONLY);
and for the server there is a channel that only write handle = gre_io_open(channel, GRE_IO_TYPE_WRONLY);

Can I open channels for writing and reading for both client and server, for the cases when I also want to send an event from server to client?

Thank you!

All the best,
Atinir
atinir
 
Posts: 25
Joined: Thu Apr 21, 2016 9:20 am

Re: Communication between CrankApp in Android and C(Linux)

Postby nschultz » Thu Mar 30, 2017 8:57 am

Hello Atinir,

You do not need to combine the lua socket module and the TCP/IP relay. The TCP/IP relay will likely involve less change overall and be a good place to start. The lua socket module can allow more flexibility in case you want to connect to things like web services.

Storyboard IO channels support communication in one direction so if you wanted bidirectional communication you would have channels for sending in each direction.

Thank you

Nik
nschultz
 
Posts: 16
Joined: Fri Jul 17, 2015 1:51 pm

Re: Communication between CrankApp in Android and C(Linux)

Postby atinir » Thu Mar 30, 2017 11:57 am

Hi Nik,

Thank you! I find all these information very helpful!

Wishing you all the best,

Aitnir
atinir
 
Posts: 25
Joined: Thu Apr 21, 2016 9:20 am


Return to Storyboard for Android

Who is online

Users browsing this forum: No registered users and 1 guest

cron