Skip to content

Commit

Permalink
Merge pull request #53 from org-arl/stp
Browse files Browse the repository at this point in the history
Updated UnetSocket C APIs
  • Loading branch information
prasadtiru authored Apr 23, 2021
2 parents 52acfd1 + 5ff6562 commit 383ab97
Show file tree
Hide file tree
Showing 20 changed files with 193,543 additions and 421 deletions.
88 changes: 75 additions & 13 deletions unetsocket/c/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
CC = gcc
CFLAGS += -std=c99 -Wall -Wextra -Werror -Wfloat-equal -Wconversion -Wparentheses -pedantic -Wunused-parameter -Wunused-variable -Wreturn-type -Wno-unused-function -Wredundant-decls -Wreturn-type -Wunused-value -Wswitch-default -Wuninitialized -Winit-self -O2
FJAGE_VER=88be0c504f98579ab947e7ec6a63debeb18961cf
#FJAGE_VER=master
FJAGE_VER=7275142c2bc846d2f07400d7d628f213cb5b56ab
FJAGE_DIR=fjage-$(FJAGE_VER)
BUILD = build
BUILD_API = $(BUILD)/api
CONTRIB_DIR = $(BUILD)/temp

all: libs
rm -rf *.zip
rm -rf fjage-*

samples: txdata rxdata range setpowerlevel
samples: txdata rxdata range setpowerlevel txsignal pbrecord bbrecord npulses wakeup rs232_wakeup
rm -rf *.zip
rm -rf fjage-*

Expand All @@ -26,6 +28,9 @@ $(FJAGE_DIR):
libunet.a: unet.o
$(AR) rc $@ $^

unet_ext.o: unet.h fjage.h
$(CC) $(CFLAGS) -c unet_ext.c -o unet_ext.o

txdata.o: samples/txdata.c unet.h fjage.h
$(CC) $(CFLAGS) -c samples/txdata.c -o samples/txdata.o

Expand All @@ -38,28 +43,85 @@ rxdata.o: samples/rxdata.c unet.h fjage.h
rxdata: rxdata.o
$(CC) -o samples/rxdata samples/rxdata.o libunet.a libfjage.a -lpthread -lm

range.o: samples/range.c unet.h fjage.h
range.o: samples/range.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/range.c -o samples/range.o

range: range.o
$(CC) -o samples/range samples/range.o libunet.a libfjage.a -lpthread -lm
range: range.o unet_ext.o
$(CC) -o samples/range unet_ext.o samples/range.o libunet.a libfjage.a -lpthread -lm

setpowerlevel.o: samples/setpowerlevel.c unet.h fjage.h
setpowerlevel.o: samples/setpowerlevel.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/setpowerlevel.c -o samples/setpowerlevel.o

setpowerlevel: setpowerlevel.o
$(CC) -o samples/setpowerlevel samples/setpowerlevel.o libunet.a libfjage.a -lpthread -lm
setpowerlevel: setpowerlevel.o unet_ext.o
$(CC) -o samples/setpowerlevel unet_ext.o samples/setpowerlevel.o libunet.a libfjage.a -lpthread -lm

txsignal.o: samples/txsignal.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/txsignal.c -o samples/txsignal.o

txsignal: txsignal.o
$(CC) -o samples/txsignal unet_ext.o samples/txsignal.o libunet.a libfjage.a -lpthread -lm

pbrecord.o: samples/pbrecord.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/pbrecord.c -o samples/pbrecord.o

pbrecord: pbrecord.o unet_ext.o
$(CC) -o samples/pbrecord unet_ext.o samples/pbrecord.o libunet.a libfjage.a -lpthread -lm

bbrecord.o: samples/bbrecord.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/bbrecord.c -o samples/bbrecord.o

bbrecord: bbrecord.o
$(CC) -o samples/bbrecord unet_ext.o samples/bbrecord.o libunet.a libfjage.a -lpthread -lm

npulses.o: samples/npulses.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/npulses.c -o samples/npulses.o

test: test_unet
npulses: npulses.o unet_ext.o
$(CC) -o samples/npulses unet_ext.o samples/npulses.o libunet.a libfjage.a -lpthread -lm

wakeup.o: samples/wakeup.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/wakeup.c -o samples/wakeup.o

wakeup: wakeup.o unet_ext.o
$(CC) -o samples/wakeup unet_ext.o samples/wakeup.o libunet.a libfjage.a -lpthread -lm

rs232_wakeup.o: samples/rs232_wakeup.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/rs232_wakeup.c -o samples/rs232_wakeup.o

rs232_wakeup: rs232_wakeup.o unet_ext.o
$(CC) -o samples/rs232_wakeup unet_ext.o samples/rs232_wakeup.o libunet.a libfjage.a -lpthread -lm

test: test_unet unet_ext.o
rm -rf *.zip
rm -rf fjage-*

test_unet.o: test/test_unet.c unet.h fjage.h
test_unet.o: test/test_unet.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c test/test_unet.c -o test/test_unet.o

test_unet: test_unet.o
$(CC) -o test/test_unet test/test_unet.o libunet.a libfjage.a -lpthread -lm
$(CC) -o test/test_unet unet_ext.o test/test_unet.o libunet.a libfjage.a -lpthread -lm

clean:
rm -rf c-api libunet.a *.o samples/*.o test/*.o test/test_unet samples/wakeup samples/rs232_wakeup samples/txdata samples/rxdata samples/range samples/setpowerlevel
rm -rf c-api libunet.a *.o samples/*.o test/*.o test/test_unet samples/wakeup samples/rs232_wakeup samples/txdata samples/rxdata samples/range samples/setpowerlevel samples/txsignal samples/pbrecord samples/bbrecordedsignal.txt samples/pbrecordedsignal.txt samples/bbrecord samples/npulses samples/gpio
rm -rf "$(FJAGE_DIR)" "$(FJAGE_VER).zip" fjage.h libfjage.a kissfft kiss_fft.h fjage
rm -rf $(BUILD)

$(CONTRIB_DIR): $(BUILD_API)
curl -LO https://github.com/org-arl/fjage/archive/$(FJAGE_VER).zip
mv $(FJAGE_VER).zip $(BUILD)/
cd $(BUILD) && unzip -a $(FJAGE_VER).zip
mkdir -p $(BUILD_API)/libs/fjage/gateways
cp -r $(BUILD)/$(FJAGE_DIR)/gateways/c $(BUILD_API)/libs/fjage/gateways/c
rsync -av --exclude '$(BUILD)' . $(BUILD_API)
rm -rf $(BUILD_API)/test
cp Makefile.package $(BUILD_API)/Makefile
rm $(BUILD_API)/Makefile.package
cd $(BUILD_API) && zip -r ../c-api.zip .
rm -rf $(BUILD_API)
rm -rf $(BUILD)/$(FJAGE_DIR)
rm -rf $(BUILD)/$(FJAGE_VER).zip

$(BUILD_API):
mkdir -p $(BUILD_API)

package: $(CONTRIB_DIR)
105 changes: 105 additions & 0 deletions unetsocket/c/Makefile.package
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
CC = gcc
CFLAGS += -std=c99 -Wall -Wextra -Werror -Wfloat-equal -Wconversion -Wparentheses -pedantic -Wunused-parameter -Wunused-variable -Wreturn-type -Wno-unused-function -Wredundant-decls -Wreturn-type -Wunused-value -Wswitch-default -Wuninitialized -Winit-self -O2

all: libs
rm -rf *.zip
rm -rf fjage-*

libs: libfjage.a libunet.a
rm -rf *.zip
rm -rf fjage-*

samples: txdata rxdata range setpowerlevel txsignal pbrecord bbrecord npulses wakeup rs232_wakeup gpio
rm -rf *.zip
rm -rf fjage-*

libfjage.a:
make -C libs/fjage/gateways/c/
cp libs/fjage/gateways/c/fjage.h .
cp libs/fjage/gateways/c/libfjage.a .

libunet.a: unet.o
$(AR) rc $@ $^

unet_ext.o: unet.h fjage.h
$(CC) $(CFLAGS) -c unet_ext.c -o unet_ext.o

txdata.o: samples/txdata.c unet.h fjage.h
$(CC) $(CFLAGS) -c samples/txdata.c -o samples/txdata.o

txdata: txdata.o
$(CC) -o samples/txdata samples/txdata.o libunet.a libfjage.a -lpthread -lm

rxdata.o: samples/rxdata.c unet.h fjage.h
$(CC) $(CFLAGS) -c samples/rxdata.c -o samples/rxdata.o

rxdata: rxdata.o
$(CC) -o samples/rxdata samples/rxdata.o libunet.a libfjage.a -lpthread -lm

range.o: samples/range.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/range.c -o samples/range.o

range: range.o unet_ext.o
$(CC) -o samples/range unet_ext.o samples/range.o libunet.a libfjage.a -lpthread -lm

setpowerlevel.o: samples/setpowerlevel.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/setpowerlevel.c -o samples/setpowerlevel.o

setpowerlevel: setpowerlevel.o unet_ext.o
$(CC) -o samples/setpowerlevel unet_ext.o samples/setpowerlevel.o libunet.a libfjage.a -lpthread -lm

txsignal.o: samples/txsignal.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/txsignal.c -o samples/txsignal.o

txsignal: txsignal.o
$(CC) -o samples/txsignal unet_ext.o samples/txsignal.o libunet.a libfjage.a -lpthread -lm

pbrecord.o: samples/pbrecord.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/pbrecord.c -o samples/pbrecord.o

pbrecord: pbrecord.o unet_ext.o
$(CC) -o samples/pbrecord unet_ext.o samples/pbrecord.o libunet.a libfjage.a -lpthread -lm

bbrecord.o: samples/bbrecord.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/bbrecord.c -o samples/bbrecord.o

bbrecord: bbrecord.o
$(CC) -o samples/bbrecord unet_ext.o samples/bbrecord.o libunet.a libfjage.a -lpthread -lm

npulses.o: samples/npulses.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/npulses.c -o samples/npulses.o

npulses: npulses.o unet_ext.o
$(CC) -o samples/npulses unet_ext.o samples/npulses.o libunet.a libfjage.a -lpthread -lm

wakeup.o: samples/wakeup.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/wakeup.c -o samples/wakeup.o

wakeup: wakeup.o unet_ext.o
$(CC) -o samples/wakeup unet_ext.o samples/wakeup.o libunet.a libfjage.a -lpthread -lm

rs232_wakeup.o: samples/rs232_wakeup.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/rs232_wakeup.c -o samples/rs232_wakeup.o

rs232_wakeup: rs232_wakeup.o unet_ext.o
$(CC) -o samples/rs232_wakeup unet_ext.o samples/rs232_wakeup.o libunet.a libfjage.a -lpthread -lm

gpio.o: samples/gpio.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c samples/gpio.c -o samples/gpio.o

gpio: gpio.o unet_ext.o
$(CC) -o samples/gpio unet_ext.o samples/gpio.o libunet.a libfjage.a -lpthread -lm

test: test_unet unet_ext.o
rm -rf *.zip
rm -rf fjage-*

test_unet.o: test/test_unet.c unet.h fjage.h unet_ext.o
$(CC) $(CFLAGS) -c test/test_unet.c -o test/test_unet.o

test_unet: test_unet.o
$(CC) -o test/test_unet unet_ext.o test/test_unet.o libunet.a libfjage.a -lpthread -lm

clean:
rm -rf c-api libunet.a *.o samples/*.o test/*.o test/test_unet samples/wakeup samples/rs232_wakeup samples/txdata samples/rxdata samples/range samples/setpowerlevel samples/txsignal samples/pbrecord samples/bbrecordedsignal.txt samples/pbrecordedsignal.txt samples/bbrecord samples/npulses samples/gpio

14 changes: 13 additions & 1 deletion unetsocket/c/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unet C APIs

This folder contains the files `unet.h` and `unet.c`. The header file includes the definition and documentation of the C APIs and the source code is provided in the `unet.c` file.
This folder contains the files `unet.h`, `unet_ext.h` and `unet.c`, `unet_ext.c`. The header files includes the definition and documentation of the C APIs and the source code is provided in the `unet.c` and `unet_ext.c` file.

The APIs defined in `unet.h` are standard UnetSocket APIs. These APIs have similar functionalities as the UnetSocket APIs provided in other languages such as [Python](https://github.com/org-arl/unet-contrib/tree/stp/unetsocket/python) and [Julia](https://github.com/org-arl/UnetSockets.jl). The APIs defined in `unet_ext.h` are extra functionalities that are implemented using the standard UnetSocket APIs. Some of these APIs in `unet_ext.h` are only supported on [Unet SDOAMs](https://unetstack.net/handbook/unet-handbook_introduction.html).

## Instructions for building and using Unet C API library on Linux / macOS

Expand Down Expand Up @@ -71,6 +73,16 @@ To clean all the built files and the depencencies, run
del *.obj *.dll unet.lib test\*.obj test\*.exe samples\*.obj samples\*.exe 2>nul
```

## Instructions for creating API package for distribution

To create a package for distribution of these APIs, simply type

```bash
make package
```

This will create a `c-api.zip` inside the `build` folder which can be used for distribution.

## Test Unet C API library

To compile and run the tests, the following steps need to be performed:
Expand Down
1 change: 1 addition & 0 deletions unetsocket/c/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
85 changes: 85 additions & 0 deletions unetsocket/c/samples/bbrecord.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
///////////////////////////////////////////////////////////////////////////////
//
// Record a baseband signal.
//
// In terminal window (an example):
//
// $ make samples
// $ ./bbrecord <ip_address> <siglen> [port]
//
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include "../unet.h"
#include "../unet_ext.h"
#include <math.h>

#ifndef _WIN32
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#endif

static int error(const char *msg) {
printf("\n*** ERROR: %s\n\n", msg);
return -1;
}

int main(int argc, char *argv[]) {
FILE *fptr;
fptr = (fopen("samples/bbrecordedsignal.txt", "w"));
if(fptr == NULL) return -1;
unetsocket_t sock;
int port = 1100;
int siglen = -1;
int rv;
if (argc <= 2) {
error("Usage : bbrecord <ip_address> <siglen> [port] \n"
"ip_address: IP address of the transmitter modem. \n"
"siglen: number of baseband samples to record. \n"
"port: port number of transmitter modem. \n"
"A usage example: \n"
"bbrecord 192.168.1.20 48000 1100\n");
return -1;
}
else if (argc >= 3) {
siglen = (int)strtol(argv[2], NULL, 10);
if (argc > 3) port = (int)strtol(argv[3], NULL, 10);
}

float buf[2*siglen];

#ifndef _WIN32
// Check valid ip address
struct hostent *server = gethostbyname(argv[1]);
if (server == NULL) {
error("Enter a valid ip addreess\n");
return -1;
}
#endif
// Open a unet socket connection to modem
printf("Connecting to %s:%d\n",argv[1],port);

sock = unetsocket_open(argv[1], port);
if (sock == NULL) return error("Couldn't open unet socket");

// Record
printf("Starting a record\n");
rv = unetsocket_ext_bbrecord(sock, buf, siglen);
if (rv == 0) {
for(int i = 0; i < 2*siglen; i++) {
fprintf(fptr,"%f\n", buf[i]);
}
} else {
error("Error recording signal");
}

// Close the unet socket
unetsocket_close(sock);

printf("\nRecording Complete (samples/bbrecordedsignal.txt saved with alternating real and imaginary values)\n");

fclose(fptr);
return 0;
}
Loading

0 comments on commit 383ab97

Please sign in to comment.