summaryrefslogtreecommitdiffstats
path: root/test/agl-service-network-ctl.c
blob: 6808b8d716b72930ebb57c7cb72e03450ba6c383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
{
  "openapi": "3.0.0",
  "$schema": "http:iot.bzh/download/openapi/schema-3.0/default-schema.json",
  "info": {
    "description": "",
    "title": "ucs2",
    "version": "1.0",
    "x-binding-c-generator": {
      "api": "UNICENS",
      "version": 2,
      "prefix": "ucs2_",
      "postfix": "",
      "start": null ,
      "onevent": null,
      "preinit": null,
      "init": "ucs2_initbinding",
      "scope": "",
      "private": false
    }
  },
  "servers": [
    {
      "url": "ws://{host}:{port}/api/monitor",
      "description": "Unicens2 API.",
      "variables": {
        "host": {
          "default": "localhost"
        },
        "port": {
          "default": "1234"
        }
      },
      "x-afb-events": [
        {
          "$ref": "#/components/schemas/afb-event"
        }
      ]
    }
  ],
  "components": {
    "schemas": {
      "afb-reply": {
        "$ref": "#/components/schemas/afb-reply-v2"
      },
      "afb-event": {
        "$ref": "#/components/schemas/afb-event-v2"
      },
      "afb-reply-v2": {
        "title": "Generic response.",
        "type": "object",
        "required": [ "jtype", "request" ],
        "properties": {
          "jtype": {
            "type": "string",
            "const": "afb-reply"
          },
          "request": {
            "type": "object",
            "required": [ "status" ],
            "properties": {
              "status": { "type": "string" },
              "info": { "type": "string" },
              "token": { "type": "string" },
              "uuid": { "type": "string" },
              "reqid": { "type": "string" }
            }
          },
          "response": { "type": "object" }
        }
      },
      "afb-event-v2": {
        "type": "object",
        "required": [ "jtype", "event" ],
        "properties": {
          "jtype": {
            "type": "string",
            "const": "afb-event"
          },
          "event": { "type": "string" },
          "data": { "type": "object" }
        }
      }
    },
    "x-permissions": {
        "config": {
          "permission": "urn:AGL:permission:UNICENS:public:initialise"
        },
        "monitor": {
          "permission": "urn:AGL:permission:UNICENS:public:monitor"
        }
    },
    "responses": {
            "200": {
              "description": "A complex object array response",
              "content": {
                "application/json": {
                  "schema": {
                    "$ref": "#/components/schemas/afb-reply"
                  }
                }
            }
          }
    }
  },
  "paths": {
    "/listconfig": {
      "description": "List Config Files",
      "get": {
        "x-permissions": {
          "$ref": "#/components/x-permissions/config"
        },
        "parameters": [
          {
            "in": "query",
            "name": "cfgpath",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {"$ref": "#/components/responses/200"}
        }
      }
    },
    "/initialise": {
      "descriptio
/*
 * Copyright (C) 2018 Konsulko Group
 * Author Pantelis Antoniou <pantelis.antoniou@konsulko.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <pthread.h>
#include <ctype.h>
#include <alloca.h>
#include <assert.h>

#include <systemd/sd-event.h>
#include <json-c/json.h>

#include <afb/afb-wsj1.h>
#include <afb/afb-ws-client.h>
#include <afb/afb-proto-ws.h>

struct state {
	const char *url;
	int port;
	const char *token;
	const char *api;
	char *uri;
	bool interactive;
	bool debug;
	bool noexit;

	sd_event *loop;
	struct afb_wsj1 *wsj1;
	const char *proto;
	bool hangup;
};

struct cmd {
	const char *verb;
	int (*call)(struct state *s, const struct cmd *c, int argc, char *argv[]);
	void (*reply)(void *closure, struct afb_wsj1_msg *msg);
};

/* print usage of the program */
/* declaration of functions */
static void on_wsj1_hangup(void *closure, struct afb_wsj1 *wsj1);
static void on_wsj1_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
static void on_wsj1_event(void *closure, const char *event, struct afb_wsj1_msg *msg);

/* the callback interface for wsj1 */
static struct afb_wsj1_itf wsj1_itf = {
	.on_hangup = on_wsj1_hangup,
	.on_call = on_wsj1_call,
	.on_event = on_wsj1_event
};

static void on_reply(struct state *s, const char *verb, struct afb_wsj1_msg *msg)
{
	printf("ON-REPLY %s: %s: %s\n%s\n",
			s->api,
			verb,
			afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
			json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
						JSON_C_TO_STRING_PRETTY));
	fflush(stdout);

	/* if non interactive terminate */
	if (!s->interactive) {
		if (!s->noexit)
			sd_event_exit(s->loop,
				afb_wsj1_msg_is_reply_ok(msg) ? 0 : EXIT_FAILURE);
		else
			printf("Ctrl-C to exit\n");
	}

}

static int do_call(struct state *s, const struct cmd *c, json_object *jparams)
{
	printf("CALL %s(%s)\n", c->verb,
			jparams ? json_object_to_json_string(jparams) : "");

	return afb_wsj1_call_j(s->wsj1, s->api, c->verb, jparams, c->reply, s);
}

static int call_void(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	return do_call(s, c, NULL);
}

static void on_ping_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "ping", msg);
}

static void on_subscribe_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "subscribe", msg);
}

static void on_unsubscribe_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "unsubscribe", msg);
}

static int call_subscribe_unsubscribe(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jobj;

	/* must give event name */
	if (argc < 1)
		return -1;
	jobj = json_object_new_object();
	json_object_object_add(jobj, "value", json_object_new_string(argv[0]));

	return do_call(s, c, jobj);
}

static void on_state_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "state", msg);
}

static void on_technologies_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "technologies", msg);
}

static void on_services_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "services", msg);
}

static void on_enable_technology_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "enable_technology", msg);
}

static void on_disable_technology_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "disable_technology", msg);
}

static int call_technology_arg(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jobj;

	if (argc < 1)
		return -1;

	jobj = json_object_new_object();
	json_object_object_add(jobj, "technology", json_object_new_string(argv[0]));

	return do_call(s, c, jobj);
}

static void on_scan_services_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "scan_services", msg);
}

static void on_offline_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "offline", msg);
}

static int call_offline(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jobj;

	/* with no arguments it's a getter */
	if (argc >= 1) {
		jobj = json_object_new_object();
		json_object_object_add(jobj, "value", json_object_new_string(argv[0]));
	} else
		jobj = NULL;

	return do_call(s, c, jobj);
}

static void on_connect_service_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "connect_service", msg);
}

static int call_service_arg(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jobj;

	if (argc < 1)
		return -1;

	jobj = json_object_new_object();
	json_object_object_add(jobj, "service", json_object_new_string(argv[0]));

	return do_call(s, c, jobj);
}

static void on_disconnect_service_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "disconnect_service", msg);
}

static void on_remove_service_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "remove_service", msg);
}

static void on_move_service_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "move_service", msg);
}

static int call_move_service(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jobj;

	/* 3 arguments and the middle must be before or after */
	if (argc < 3 || (strcmp(argv[1], "before") && strcmp(argv[1], "after")))
		return -1;

	jobj = json_object_new_object();
	json_object_object_add(jobj, "service", json_object_new_string(argv[0]));
	json_object_object_add(jobj,
			!strcmp(argv[1], "before") ? "before_service" : "after_service",
			json_object_new_string(argv[2]));

	return do_call(s, c, jobj);
}

static void on_set_property_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "set_property", msg);
}

static void on_get_property_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "get_property", msg);
}

static bool get_property_obj(json_object *jparent, int argc, char *argv[])
{
	json_object *jobj, *jobj2;
	int i, j, nest;

	for (i = 0; i < argc; i++) {

		if ((i < (argc - 1) && !strcmp(argv[i+1], "{"))) {
			nest = 1;
			for (j = i + 2; j < argc; j++) {
				if (!strcmp(argv[j], "{"))
					nest++;
				else if (!strcmp(argv[j], "}"))
					nest--;

				if (!nest)
					break;
			}
			if (j >= argc)
				return false;

			jobj = json_object_new_object();

			jobj2 = json_object_new_array();
			get_property_obj(jobj2, j - i - 2, argv + i + 2);
			json_object_object_add(jobj, argv[i], jobj2);
			i = j;
		} else
			jobj = json_object_new_string(argv[i]);

		json_object_array_add(jparent, jobj);

	}

	return true;
}

struct json_object *get_property_value(const char *str)
{
	char *le, *de;
	long long ll;
	double d;

	if (!strcmp(str, "null"))
		return NULL;
	if (!strcmp(str, "true"))
	       return json_object_new_boolean(true);
	if (!strcmp(str, "false"))
	       return json_object_new_boolean(false);

	/* try both double and long and see which one is furthest */
	ll = strtoll(str, &le, 0);
	d = strtod(str, &de);

	if (de > le) {
		while (isspace(*de))
			de++;
		if (!*de)
			return json_object_new_double(d);
	} else {
		while (isspace(*le))
			le++;
		if (!*le)
			return json_object_new_int64((int64_t)ll);
	}

	/* everything else is a string */
	return json_object_new_string(str);
}

/* how many args is a single json object value */
static int next_span(int pos, int argc, char *argv[])
{
	const char *left, *right;
	int j, nest;

	if (pos >= argc)
		return 0;

	if (!strcmp(argv[pos], "{") || !strcmp(argv[pos], "[")) {

		if (!strcmp(argv[pos], "{")) {
			left = "{";
			right = "}";
		} else {
			left = "[";
			right = "]";
		}
		nest = 1;
		for (j = pos + 1; nest > 0 && j < argc; j++) {
			if (!strcmp(argv[j], left))
				nest++;
			else if (!strcmp(argv[j], right))
				nest--;
		}
		if (nest && j >= argc) {
			fprintf(stderr, "nesting error\n");
			return -1;
		}

		return j - pos;
	}
	return 1;
}

bool add_property_value(json_object *jparent, const char *key, int argc, char *argv[])
{
	json_object *jobj;
	const char *key2;
	int i, span;

	if (!strcmp(argv[0], "[")) {
		assert(!strcmp(argv[argc - 1], "]"));

		argc -= 2;
		argv++;

		jobj = json_object_new_array();

		for (i = 0; i < argc; ) {
			span = next_span(i, argc, argv);
			if (span < 0) {
				fprintf(stderr, "bad nesting\n");
				return NULL;
			}

			if (!add_property_value(jobj, NULL, span, argv + i)) {
				fprintf(stderr, "error adding array\n");
				return false;
			}
			i += span;

		}
	} else if (!strcmp(argv[0], "{")) {
		assert(!strcmp(argv[argc - 1], "}"));

		argc -= 2;
		argv++;

		jobj = json_object_new_object();

		for (i = 0; i < argc; ) {
			key2 = argv[i];

			span = next_span(i + 1, argc, argv);
			if (span < 0) {
				fprintf(stderr, "bad nesting\n");
				return NULL;
			}

			if (!add_property_value(jobj, key2, span, argv + i + 1)) {
				fprintf(stderr, "error adding object\n");
				return false;
			}
			i += 1 + span;
		}
	} else {
		jobj = get_property_value(argv[0]);
	}

	if (key)
		json_object_object_add(jparent, key, jobj);
	else
		json_object_array_add(jparent, jobj);

	return true;
}

bool set_property_obj(json_object *jparent, int argc, char *argv[])
{
	int i, span;
	const char *key;

	for (i = 0; i < argc; ) {

		key = argv[i];
		if (!strcmp(key, "{") || !strcmp(key, "[")) {
			fprintf(stderr, "Bad object start {\n");
			return false;
		}
		i++;

		if (i > (argc - 1)) {
			fprintf(stderr, "out of arguments on key %s\n", key);
			return false;
		}

		span = next_span(i, argc, argv);
		if (span < 0) {
			fprintf(stderr, "bad nesting\n");
			return false;
		}

		if (!add_property_value(jparent, key, span, argv + i)) {
			fprintf(stderr, "error adding property\n");
			return false;
		}
		i += span;
	}

	return true;
}

static int call_get_set_property(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jobj, *jprop = NULL;
	const char *type;
	int min_argc;

	if (argc < 1)
		return -1;

	type = argv[0];
	/* global or technology or service */
	if (strcmp(type, "global") && strcmp(type, "technology") &&
			strcmp(type, "service")) {
		fprintf(stderr, "unknown property type %s\n", type);
		return -1;
	}

	/* minimum is get global */
	min_argc = 1;

	/* technology or service have extra argument */
	if (!strcmp(type, "technology") || !strcmp(type, "service"))
		min_argc++;

	/* set property must have an argument */
	min_argc += !strcmp(c->verb, "set_property") ? 2 : 0;

	if (argc < min_argc) {
		fprintf(stderr, "not enough arguments\n");
		return -1;
	}

	jobj = json_object_new_object();

	argc--;
	argv++;

	if (!strcmp(type, "technology")) {
		json_object_object_add(jobj, "technology",
				json_object_new_string(argv[0]));
		argc--;
		argv++;
	} else if (!strcmp(type, "service")) {
		json_object_object_add(jobj, "service",
				json_object_new_string(argv[0]));
		argc--;
		argv++;
	}

	/* get is array, set is object */
	if (!strcmp(c->verb, "get_property")) {
		if (argc > 0) {
			jprop = json_object_new_array();
			get_property_obj(jprop, argc, argv);
		}
	} else {
		jprop = json_object_new_object();
		set_property_obj(jprop, argc, argv);
	}

	if (jprop)
		json_object_object_add(jobj, "properties", jprop);

	return do_call(s, c, jobj);
}

static void on_agent_response_reply(void *closure, struct afb_wsj1_msg *msg)
{
	on_reply(closure, "agent_response", msg);
}

static int call_agent_response(struct state *s, const struct cmd *c, int argc, char *argv[])
{
	json_object *jresp = NULL, *jprop = NULL;
	const char *method;
	const char *propname;
	int id;

	if (argc < 3)
		return -1;

	method = *argv++;
	argc--;

	id = (int)strtol(*argv++, NULL, 10);
	argc--;

	if (id <= 0) {
		fprintf(stderr, "bad agent response id %d\n", id);
		return -1;
	}

	propname = NULL;
	if (!strcmp(method, "request-input")) {
		propname = "fields";
	} else {
		fprintf(stderr, "unknown agent response method '%s'\n",
			method);
		return -1;
	}

	jresp = json_object_new_object();
	json_object_object_add(jresp, "method",
			json_object_new_string(method));
	json_object_object_add(jresp, "id",
			json_object_new_int(id));

	if (argc > 0) {
		jprop = json_object_new_object();
		set_property_obj(jprop, argc, argv);
		json_object_object_add(jresp, propname, jprop);
	}

	return do_call(s, c, jresp);
}


static const struct cmd cmds[] = {
	{
		.verb	= "ping",
		.call	= call_void,
		.reply	= on_ping_reply,
	}, {
		.verb	= "subscribe",
		.call	= call_subscribe_unsubscribe,
		.reply	= on_subscribe_reply,
	}, {
		.verb	= "unsubscribe",
		.call	= call_subscribe_unsubscribe,
		.reply	= on_unsubscribe_reply,
	}, {
		.verb	= "state",
		.call	= call_void,
		.reply	= on_state_reply,
	}, {
		.verb	= "technologies",
		.call	= call_void,
		.reply	= on_technologies_reply,
	}, {
		.verb	= "services",
		.call	= call_void,
		.reply	= on_services_reply,
	}, {
		.verb	= "enable_technology",
		.call	= call_technology_arg,
		.reply	= on_enable_technology_reply,
	}, {
		.verb	= "disable_technology",
		.call	= call_technology_arg,
		.reply	= on_disable_technology_reply,
	}, {
		.verb	= "scan_services",
		.call	= call_technology_arg,
		.reply	= on_scan_services_reply,
	}, {
		.verb	= "offline",
		.call	= call_offline,
		.reply	= on_offline_reply,
	}, {
		.verb	= "connect_service",
		.call	= call_service_arg,
		.reply	= on_connect_service_reply,
	}, {
		.verb	= "disconnect_service",
		.call	= call_service_arg,
		.reply	= on_disconnect_service_reply,
	}, {
		.verb	= "remove_service",
		.call	= call_service_arg,
		.reply	= on_remove_service_reply,
	}, {
		.verb	= "move_service",
		.call	= call_move_service,
		.reply	= on_move_service_reply,
	}, {
		.verb	= "set_property",
		.call	= call_get_set_property,
		.reply	= on_set_property_reply,
	}, {
		.verb	= "get_property",
		.call	= call_get_set_property,
		.reply	= on_get_property_reply,
	}, {
		.verb	= "agent_response",
		.call	= call_agent_response,
		.reply	= on_agent_response_reply,
	},
	{ }
};

static int call_cmd(struct state *s, int argc, char *argv[])
{
	const struct cmd *c;
	const char *verb;

	/* first argument is verb */
	if (argc < 1 || !argv[0]) {
		if (!s->interactive)
			fprintf(stderr, "No verb given\n");
		goto out_err;
	}
	verb = argv[0];
	argv++;
	argc--;

	for (c = cmds; c->verb; c++) {
		if (!strcmp(verb, c->verb))
			return c->call(s, c, argc, argv);
	}

	if (!s->interactive)
		fprintf(stderr, "Unknown API verb \"%s\"\n", verb);
out_err:

	if (!s->interactive)
		sd_event_exit(s->loop, EXIT_FAILURE);

	return -1;
}

static struct option opts[] = {
	{ "url",	required_argument,	0, 'u' },
	{ "port",	required_argument,	0, 'p' },
	{ "token",	required_argument,	0, 't' },
	{ "api",	required_argument,	0, 'a' },
	{ "noexit",	no_argument,		0, 'x' },
	{ "debug",	no_argument,		0, 'd' },
	{ "help",	no_argument,		0, 'h' },
	{ }
};

#define DEFAULT_URL	"ws://localhost"
#define DEFAULT_PORT	1234
#define DEFAULT_TOKEN	"HELLO"
#define DEFAULT_API	"network-manager"
#define DEFAULT_DEBUG	true

static void usage(int status, const char *arg0)
		__attribute__((__noreturn__));

static void usage(int status, const char *arg0)
{
	const char *name = strrchr(arg0, '/');
	FILE *outf = status ? stderr : stdout;

	name = name ? name + 1 : arg0;

	fprintf(outf, "usage: %s <options> [arguments]\n", name);
	fprintf(outf, "  options are:\n");
	fprintf(outf, "    -u, --url=X          URL to connect to (default %s)\n", DEFAULT_URL);
	fprintf(outf, "    -p, --port=X         Port to use for connection (default %d)\n", DEFAULT_PORT);
	fprintf(outf, "    -t, --token=X        Token to use (default %s)\n", DEFAULT_TOKEN);
	fprintf(outf, "    -a, --api=X          API to use (default %s)\n", DEFAULT_API);
	fprintf(outf, "    -x, --noexit         Do not exit in non-interactive mode (events)\n");
	fprintf(outf, "    -d, --debug          Enable debug printouts\n");
	fprintf(outf, "    -h, -?, --help       Display this help\n");

	exit(status);
}

/* entry function */
int main(int argc, char **argv)
{
	int rc, ret, cc = 0, option_index;
	const char *name;
	struct state state, *s = &state;

	memset(s, 0, sizeof(*s));
	s->url = DEFAULT_URL;
	s->port = DEFAULT_PORT;
	s->token = DEFAULT_TOKEN;
	s->api = DEFAULT_API;
	s->debug = DEFAULT_DEBUG;

	s->interactive = false;
	s->noexit = false;

	while ((cc = getopt_long(argc, argv, "u:p:t:a:xdh?", opts,
					&option_index)) != -1) {

		if (cc == 0 && option_index >= 0) {
			name = opts[option_index].name;
			if (!name)
				continue;
			/* we don't have long options without short ones */
			usage(EXIT_FAILURE, argv[0]);
		}

		switch (cc) {
		case 'u':
			s->url = optarg;
			break;
		case 'p':
			s->port = atoi(optarg);
			break;
		case 't':
			s->token = optarg;
			break;
		case 'a':
			s->api = optarg;
			break;
		case 'x':
			s->noexit = true;
			break;
		case 'd':
			s->debug = true;
			break;
		case 'h':
		case '?':
			usage(0, argv[0]);
			break;
		}
	}

	/* no arguments left, interactive mode */
	if (optind >= argc) {
		printf("optind=%d\n", optind);
		printf("argc=%d\n", argc);
		s->interactive = true;
	}

	ret = EXIT_FAILURE;

	rc = asprintf(&s->uri, "%s:%d/api?token=%s", s->url, s->port, s->token);
	if (rc < 0) {
		fprintf(stderr, "Unable to build URI\n");
		goto out;
	}

	if (s->debug) {
		fprintf(stderr, "URI:         \"%s\"\n", s->uri);
		fprintf(stderr, "API:         \"%s\"\n", s->api);
		fprintf(stderr, "interactive: %s\n", s->interactive ? "true" : "false");
		fprintf(stderr, "noexit:      %s\n", s->noexit ? "true" : "false");
	}

	/* get the default event loop */
	rc = sd_event_default(&s->loop);
	if (rc < 0) {
		fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
		goto out_no_event;
	}

	/* connect the websocket wsj1 to the uri given by the first argument */
	s->wsj1 = afb_ws_client_connect_wsj1(s->loop, s->uri, &wsj1_itf, s);
	if (s->wsj1 == NULL) {
		fprintf(stderr, "connection to %s failed: %m\n", argv[1]);
		goto out_no_wsj1;
	}

	if (!s->interactive) {
		ret = call_cmd(s, argc - optind, argv + optind);
		if (ret < 0) {
			fprintf(stderr, "command failed\n");
			sd_event_exit(s->loop, EXIT_FAILURE);
		}
	} else {
		fprintf(stderr, "interactive mode not yet supported\n");
		sd_event_exit(s->loop, EXIT_FAILURE);
	}

	ret = sd_event_loop(s->loop);

	/* cleanup */

	afb_wsj1_unref(s->wsj1);
out_no_wsj1:
	sd_event_unref(s->loop);
out_no_event:
	free(s->uri);
out:
	return ret;
}

/* called when wsj1 hangsup */
static void on_wsj1_hangup(void *closure, struct afb_wsj1 *wsj1)
{
	struct state *s = closure;

	printf("ON-HANGUP\n");
	fflush(stdout);

	s->hangup = true;
	sd_event_exit(s->loop, 0);
}

/* called when wsj1 receives a method invocation */
static void on_wsj1_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
	int rc;

	printf("ON-CALL %s/%s:\n%s\n", api, verb,
			json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
						JSON_C_TO_STRING_PRETTY));
	fflush(stdout);
	rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
	if (rc < 0)
		fprintf(stderr, "replying failed: %m\n");
}

/* called when wsj1 receives an event */
static void on_wsj1_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
	printf("ON-EVENT %s:\n%s\n", event,
			json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
						JSON_C_TO_STRING_PRETTY));
	fflush(stdout);
}