Skip to content

Commit b55a3e0

Browse files
committed
WIP: split the e4cli.c file up into multiple parts.
Specific recv.c for receive processing (so it is clear for documentation purposes). clang-format integration + use Implementation of some of the functions. This code is very much a work-in-progress.
1 parent f9e9aa2 commit b55a3e0

14 files changed

+578
-311
lines changed

.clang

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
-I../lib-c/include/
2-
-I./src/
1+
flags = -I$(pwd)/../libe4/include/ -Wall -Werror -std=c99

.clang-format

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
AccessModifierOffset: 0
2+
AlignEscapedNewlinesLeft: false
3+
AlignTrailingComments: true
4+
AllowAllParametersOfDeclarationOnNextLine: false
5+
AllowShortFunctionsOnASingleLine: true
6+
AllowShortIfStatementsOnASingleLine: true
7+
AllowShortLoopsOnASingleLine: true
8+
AlwaysBreakBeforeMultilineStrings: false
9+
AlwaysBreakTemplateDeclarations: false
10+
BinPackParameters: false
11+
BreakBeforeBinaryOperators: false
12+
BreakBeforeBraces: Allman
13+
BreakBeforeTernaryOperators: false
14+
BreakConstructorInitializersBeforeComma: false
15+
ColumnLimit: 80
16+
CommentPragmas: ''
17+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
18+
ConstructorInitializerIndentWidth: 0
19+
ContinuationIndentWidth: 0
20+
Cpp11BracedListStyle: false
21+
DerivePointerBinding: false
22+
IndentCaseLabels: false
23+
IndentFunctionDeclarationAfterType: false
24+
IndentWidth: 4
25+
Language: Cpp
26+
MaxEmptyLinesToKeep: 2
27+
NamespaceIndentation: None
28+
ObjCSpaceAfterProperty: true
29+
ObjCSpaceBeforeProtocolList: true
30+
PenaltyBreakBeforeFirstCallParameter: 100
31+
PenaltyBreakComment: 100
32+
PenaltyBreakFirstLessLess: 0
33+
PenaltyBreakString: 100
34+
PenaltyExcessCharacter: 1
35+
PenaltyReturnTypeOnItsOwnLine: 20
36+
SpaceBeforeAssignmentOperators: true
37+
SpaceBeforeParens: ControlStatements
38+
SpaceInEmptyParentheses: false
39+
SpacesBeforeTrailingComments: 1
40+
SpacesInAngles: false
41+
SpacesInCStyleCastParentheses: false
42+
SpacesInContainerLiterals: false
43+
SpacesInParentheses: false
44+
Standard: Cpp11
45+
TabWidth: 4
46+
UseTab: Never

Makefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ BIN = $(BINDIR)/e4cli
2424
LIB = $(LIBDIR)/$(LIBNAME).a
2525
DISTDIR = dist
2626

27-
OBJS = $(OBJDIR)/e4cli.o \
28-
$(OBJDIR)/mqtt.o #\
27+
OBJS = $(OBJDIR)/commands.o \
28+
$(OBJDIR)/repl.o \
29+
$(OBJDIR)/recv.o \
30+
$(OBJDIR)/mqtt.o \
31+
$(OBJDIR)/e4cli.o
2932

3033
default: setup $(BIN)
3134

@@ -48,3 +51,6 @@ clean:
4851
dist: $(BIN)
4952
@echo 'Making $(DISTDIR)/$(LIBNAME)-$(NOW)-$(GITCOMMIT).tar.bz2'
5053
tar cfvj $(DISTDIR)/$(LIBNAME)-$(NOW)-$(GITCOMMIT).tar.bz2 $(LIBDIR)/* $(INCDIR)/*
54+
55+
format:
56+
clang-format -i src/*.c src/*.h

scripts/devenv.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
## -- -- ...
1717

1818
E4LIBPARENT="$(dirname $PWD)"
19-
export E4LIBDIR="$E4LIBPARENT/lib-c/"
19+
export E4LIBDIR="$E4LIBPARENT/libe4/"
2020

2121
echo "E4LIBDIR=$E4LIBDIR"
2222

src/commands.c

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
#include "MQTTClient.h"
3+
4+
/* Use the file storage command */
5+
#define E4_STORE_FILE
6+
#include "e4/e4.h"
7+
#include "e4/strlcpy.h"
8+
#include "e4/util.h"
9+
10+
/* local header includes */
11+
#include "e4cli.h"
12+
13+
14+
void client_setid(e4client *client, const char *arg)
15+
{
16+
uint8_t id[E4_ID_LEN];
17+
size_t arglen = strlen(arg);
18+
int result = 0;
19+
20+
memset(id, 0, sizeof id);
21+
22+
if (arglen == 0 || arglen != E4_ID_LEN * 2)
23+
{
24+
printf("Expected client ID of length %d, not received.\n", E4_ID_LEN * 2);
25+
return;
26+
}
27+
28+
result = e4c_hex_decode((char*)id, sizeof(id), (char*)arg, arglen);
29+
if (result == 0)
30+
{
31+
printf("Unable to decode ID: invalid data.\n");
32+
return;
33+
}
34+
35+
client->store.e4c_set_id(client->store, id);
36+
return;
37+
}
38+
39+
void client_genkey(const char *arg)
40+
{
41+
42+
// do not generate but ask the C2 to do it?
43+
// can clients do this?
44+
// TODO: resolve above quesitons
45+
46+
printf("ERROR: not implemented in this client.\n");
47+
}
48+
49+
50+
void client_setalias(e4client *client, const char *arg)
51+
{
52+
53+
char id[E4_ID_LEN] = { 0 };
54+
55+
e4c_derive_clientid(id, sizeof id, arg, arglen);
56+
memcpy(client->store.id, id, E4_ID_LEN);
57+
e4c_sync(client->store);
58+
}
59+
60+
// sets the client key to a given value when supplied on the
61+
// command line.
62+
void client_setkey(e4client *client, const char *arg)
63+
{
64+
char keybuffer[E4_KEY_LEN];
65+
size_t keylen = strlen(arg);
66+
if (keylen != E4_KEY_LEN * 2)
67+
{
68+
printf("ERROR: key length must be %d characters, encoded in hex.\n", E4_KEY_LEN * 2);
69+
return;
70+
}
71+
e4c_hex_decode(keybuffer, E4_KEY_LEN, arg, keylen);
72+
e4c_set_idkey(client->store, keybuffer);
73+
}
74+
75+
void client_settopickey(const char *arg)
76+
{
77+
char keybuffer[E4_KEY_LEN];
78+
char *topic = arg;
79+
char *key = arg;
80+
81+
key = strchr(arg, ' ');
82+
if (key == NULL || key == arg)
83+
{
84+
printf("ERROR: must specify a topic and key in the format:\n");
85+
printf(" <topicname> <key>\n");
86+
printf(" key must be %d hexadecimal characters.", 2 * E4_KEY_LEN);
87+
return;
88+
}
89+
90+
// tokenize the strings.
91+
key[0] = '\0';
92+
key += 1;
93+
94+
size_t keylen = strlen(key);
95+
if (keylen != E4_KEY_LEN * 2)
96+
{
97+
printf("ERROR: key length must be %d characters, encoded in hex.\n", E4_KEY_LEN * 2);
98+
return;
99+
}
100+
e4c_hex_decode(keybuffer, E4_KEY_LEN, key, keylen);
101+
e4c_set_topic_key(client->store, keybuffer);
102+
}
103+
104+
void client_setpwd(const char *arg)
105+
{
106+
printf("ERROR: The C client does not yet implement password-derivation.\n");
107+
printf(" Please use the setkey command.\n");
108+
}
109+
110+
111+
void client_subscribe(const char *arg)
112+
{
113+
114+
size_t arglen = strlen(arg);
115+
if (arglen == 0) i
116+
{
117+
printf("client_subscribe: Invalid topic");
118+
return;
119+
}
120+
121+
MQTTClient_subscribe(handle, arg, 0);
122+
}
123+
124+
125+
void client_unsubscribe(const char *arg)
126+
{
127+
size_t arglen = strlen(arg);
128+
if (arglen == 0)
129+
{
130+
printf("client_subscribe: Invalid topic");
131+
return;
132+
}
133+
134+
MQTTClient_unsubscribe(handle, arg);
135+
}
136+
137+
void client_changetopic(const char *arg) {}
138+
139+
void client_e4msg(const char *arg) {}
140+
141+
void client_clearmsg(const char *arg) {}
142+
143+
void client_list(const char *arg) {}
144+
145+
void client_zero(const char *arg) {}

src/commands.h

Whitespace-only changes.

0 commit comments

Comments
 (0)