Browse Source

Add yarl submodule, build it as a dependency; add yarl test code for initial smoke test

master
Ryan Joseph 10 months ago
parent
commit
faf8d38773
7 changed files with 163 additions and 36 deletions
  1. +3
    -0
      .gitmodules
  2. +9
    -0
      spheremon.sln
  3. +3
    -3
      spheremon/app_manifest.json
  4. +140
    -26
      spheremon/main.c
  5. +7
    -2
      spheremon/spheremon.vcxproj
  6. +0
    -5
      spheremon/spheremon.vcxproj.filters
  7. +1
    -0
      yarl

+ 3
- 0
.gitmodules View File

@@ -0,0 +1,3 @@
[submodule "yarl"]
path = yarl
url = https://github.com/electric-sheep-co/yarl.git

+ 9
- 0
spheremon.sln View File

@@ -4,6 +4,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 15.0.28307.757
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "spheremon", "spheremon\spheremon.vcxproj", "{749046E9-2679-4C14-9734-08EBC9EA4322}"
ProjectSection(ProjectDependencies) = postProject
{6E98C738-2F73-41B2-A0FE-79B0588FA810} = {6E98C738-2F73-41B2-A0FE-79B0588FA810}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "yarl", "yarl\azuresphere\yarl.vcxproj", "{6E98C738-2F73-41B2-A0FE-79B0588FA810}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +20,10 @@ Global
{749046E9-2679-4C14-9734-08EBC9EA4322}.Debug|ARM.Build.0 = Debug|ARM
{749046E9-2679-4C14-9734-08EBC9EA4322}.Release|ARM.ActiveCfg = Release|ARM
{749046E9-2679-4C14-9734-08EBC9EA4322}.Release|ARM.Build.0 = Release|ARM
{6E98C738-2F73-41B2-A0FE-79B0588FA810}.Debug|ARM.ActiveCfg = Debug|ARM
{6E98C738-2F73-41B2-A0FE-79B0588FA810}.Debug|ARM.Build.0 = Debug|ARM
{6E98C738-2F73-41B2-A0FE-79B0588FA810}.Release|ARM.ActiveCfg = Release|ARM
{6E98C738-2F73-41B2-A0FE-79B0588FA810}.Release|ARM.Build.0 = Release|ARM
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE


+ 3
- 3
spheremon/app_manifest.json View File

@@ -3,10 +3,10 @@
"Name" : "spheremon",
"ComponentId" : "f54ee4c5-40a0-4f65-a7b7-acc9ad62bea6",
"EntryPoint": "/bin/app",
"CmdArgs": [],
"CmdArgs": [ "192.168.1.252", "6379", "" ],
"Capabilities": {
"AllowedConnections": [],
"Gpio": [ 9 ],
"AllowedConnections": [ "192.168.1.252" ],
"Gpio": [],
"Uart": [],
"WifiConfig": false
},


+ 140
- 26
spheremon/main.c View File

@@ -1,38 +1,152 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <applibs/log.h>
#include <applibs/gpio.h>
#include <applibs/networking.h>

int main(void)
#include <yarl.h>

// adapted from http://beej.us/guide/bgnet/html/multi/clientserver.html#simpleclient

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in *)sa)->sin_addr);
}

return &(((struct sockaddr_in6 *)sa)->sin6_addr);
}

RedisConnection_t RedisConnect(const char *host, const char *port)
{
// This minimal Azure Sphere app repeatedly toggles GPIO 9, which is the green channel of RGB
// LED 1 on the MT3620 RDB.
// Use this app to test that device and SDK installation succeeded that you can build,
// deploy, and debug an app with Visual Studio, and that you can deploy an app over the air,
// per the instructions here: https://docs.microsoft.com/azure-sphere/quickstarts/qs-overview
//
// It is NOT recommended to use this as a starting point for developing apps; instead use
// the extensible samples here: https://github.com/Azure/azure-sphere-samples
Log_Debug(
"\nVisit https://github.com/Azure/azure-sphere-samples for extensible samples to use as a "
"starting point for full applications.\n");

int fd = GPIO_OpenAsOutput(9, GPIO_OutputMode_PushPull, GPIO_Value_High);
if (fd < 0) {
Log_Debug(
"Error opening GPIO: %s (%d). Check that app_manifest.json includes the GPIO used.\n",
strerror(errno), errno);
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if ((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return -1;
}

const struct timespec sleepTime = {1, 0};
while (true) {
GPIO_SetValue(fd, GPIO_Value_Low);
nanosleep(&sleepTime, NULL);
GPIO_SetValue(fd, GPIO_Value_High);
nanosleep(&sleepTime, NULL);
// loop through all the results and connect to the first we can
for (p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
perror("client: socket");
continue;
}

if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("client: connect");
continue;
}

break;
}

if (p == NULL)
{
fprintf(stderr, "client: failed to connect\n");
return -2;
}

inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
freeaddrinfo(servinfo); // all done with this structure
return (RedisConnection_t)sockfd;
}

#define TEST_KEY_NAME "SphereMonSaysHi"

bool netCheck(void);

bool netCheck()
{
bool netUp = false;
return Networking_IsNetworkingReady(&netUp) != -1 && netUp;
}

int main(int argc, char** argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: %s host port password\n\n", argv[0]);
exit(-1);
}

const char* host = argv[1];
const char* port = argv[2];
const char* pass = argc > 3 ? argv[3] : NULL;

if (!netCheck())
{
perror("Networking Is Not Ready");
exit(-errno);
}

RedisConnection_t rConn = RedisConnect(host, port);

if (rConn < 1)
{
fprintf(stderr, "RedisConnect failed: %d\n", rConn);
exit(rConn);
}

if (pass && !Redis_AUTH(rConn, pass))
{
fprintf(stderr, "AUTH failed\n");
exit(-1);
}

bool exists = Redis_EXISTS(rConn, TEST_KEY_NAME);
printf("Exists already? %d\n", exists);

if (exists && !Redis_DEL(rConn, TEST_KEY_NAME))
{
fprintf(stderr, "DEL failed\n");
exit(-2);
}

if (!Redis_SET(rConn, TEST_KEY_NAME, "Hello, World!"))
{
fprintf(stderr, "SET failed\n");
exit(-2);
}

char* getVal = Redis_GET(rConn, TEST_KEY_NAME);
printf("Hello, World? %s\n", getVal);
free(getVal);

int appended = Redis_APPEND(rConn, TEST_KEY_NAME, " It's a beautiful day!");
getVal = Redis_GET(rConn, TEST_KEY_NAME);
printf("Appended %d bytes, now have '%s'\n", appended, getVal);
free(getVal);

if (!Redis_DEL(rConn, TEST_KEY_NAME))
{
fprintf(stderr, "DEL failed\n");
exit(-2);
}

printf("Publish result: %d\n", Redis_PUBLISH(rConn, TEST_KEY_NAME, "Pub/sub is the bee's knees!"));
}

+ 7
- 2
spheremon/spheremon.vcxproj View File

@@ -36,6 +36,10 @@
<ImportGroup Label="Shared" />
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
<IncludePath>$(MSBuildProjectDirectory)\..\yarl\src\;$(IncludePath)</IncludePath>
<LibraryPath>$(MSBuildProjectDirectory)\..\yarl\azuresphere\bin\$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemGroup>
<ClCompile Include="main.c" />
<UpToDateCheckInput Include="app_manifest.json" />
@@ -47,8 +51,9 @@
<AdditionalOptions>-Werror=implicit-function-declaration %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<LibraryDependencies>applibs;pthread;gcc_s;c</LibraryDependencies>
<LibraryDependencies>applibs;pthread;gcc_s;c;yarl</LibraryDependencies>
<AdditionalOptions>-Wl,--no-undefined -nodefaultlibs %(AdditionalOptions)</AdditionalOptions>
<AdditionalLibraryDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">$(MSBuildProjectDirectory)\..\yarl\azuresphere\bin\$(Platform)\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
</Project>
</Project>

+ 0
- 5
spheremon/spheremon.vcxproj.filters View File

@@ -19,10 +19,5 @@
<Filter>Source Files</Filter>
</ClCompile>
<UpToDateCheckInput Include="app_manifest.json" />
</ItemGroup>
<ItemGroup>
<Image Include="Icon.ico">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project>

+ 1
- 0
yarl

@@ -0,0 +1 @@
Subproject commit 99cd7f9d9f6a2367e0a47f74f3bf30aabc986a99

Loading…
Cancel
Save