boost beast sample to send post - post

I'm getting the following response and I don't know how to identify the problem. Fiddler doesn't capture anything so I believe the request is not being sent out.
HTTP/1.1 411 Length Required
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 22 May 2019 11:15:04 GMT
Connection: close
Content-Length: 344
I've attempted to follow the other examples I've found, but it seems setting the body no longer compiles.
// error C2679: binary '=': no operator found which takes a right-hand operand of type 'const char *' (or there is no acceptable conversion)
req_.body() = "test";
I'm using Visual Studio 2017 compiled for x64 with Boost linked as DLLs. I started with the beast samples and got "GET" to work perfectly for me. I'm having problems getting "POST" to work from the beast client
`
//
// Example: HTTP client, asynchronous
//
// Quickly add boost DLLs with: https://www.nuget.org/packages/boost-vc141/
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/strand.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/lexical_cast.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
// Report a failure
void
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Performs an HTTP GET and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::dynamic_body> req_;
http::response<http::string_body> res_;
public:
// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
explicit
session(net::io_context& ioc)
: resolver_(net::make_strand(ioc))
, stream_(net::make_strand(ioc))
{
}
// Start the asynchronous operation
void
run(
char const* host,
char const* port,
char const* target,
char const* body,
int version)
{
// Set up an HTTP POST request message
req_.version(version);
req_.method(http::verb::post);
req_.target(target);
req_.set(http::field::host, host);
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req_.set(http::field::content_length, boost::lexical_cast<std::string>(strlen(body)));
req_.set(http::field::body, body);
req_.prepare_payload();
// following line doesn't compile:
// error C2679: binary '=': no operator found which takes a right-hand operand of type 'const char *' (or there is no acceptable conversion)
//req_.body() = body;
// Look up the domain name
resolver_.async_resolve(
host,
port,
beast::bind_front_handler(
&session::on_resolve,
shared_from_this()));
}
void
on_resolve(
beast::error_code ec,
tcp::resolver::results_type results)
{
if (ec)
return fail(ec, "resolve");
// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
stream_.async_connect(
results,
beast::bind_front_handler(
&session::on_connect,
shared_from_this()));
}
void
on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{
if (ec)
return fail(ec, "connect");
// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(30));
// Send the HTTP request to the remote host
http::async_write(stream_, req_,
beast::bind_front_handler(
&session::on_write,
shared_from_this()));
}
void
on_write(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if (ec)
return fail(ec, "write");
// Receive the HTTP response
http::async_read(stream_, buffer_, res_,
beast::bind_front_handler(
&session::on_read,
shared_from_this()));
}
void
on_read(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if (ec)
return fail(ec, "read");
// Write the message to standard out
std::cout << res_ << std::endl;
// Gracefully close the socket
stream_.socket().shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes so don't bother reporting it.
if (ec && ec != beast::errc::not_connected)
return fail(ec, "shutdown");
// If we get here then the connection is closed gracefully
}
};
std::string create_body()
{
boost::property_tree::ptree tree;
tree.put("foo", "bar");
std::basic_stringstream<char> jsonStream;
boost::property_tree::json_parser::write_json(jsonStream, tree, false);
return jsonStream.str();
}
int main(int argc, char** argv)
{
// Check command line arguments.
if (argc != 4 && argc != 5)
{
std::cerr <<
"Usage: http-client-async <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
"Example:\n" <<
" http-client-async www.example.com 80 /\n" <<
" http-client-async www.example.com 80 / 1.0\n";
return EXIT_FAILURE;
}
auto const host = argv[1];
auto const port = argv[2];
auto const target = argv[3];
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
net::io_context ioc;
// Launch the asynchronous operation
std::make_shared<session>(ioc)->run(host, port, target, create_body().c_str(), version);
// Run the I/O service. The call will return when
// the get operation is complete.
ioc.run();
return EXIT_SUCCESS;
}
`
How do I see what beast is generating as the packet to be sent?
How do I fix the compilation error attempting to set the body as shown in other posts: request.body() = "bodytext";
Can someone provide a sample beast client & server that uses post?

In the operator= for the body is not available because your request is declared with template http::dynamic_body:
http::request<http::dynamic_body> req_;
Change your template argument to http::string_body and the operator= will work
http::response<http::string_body> req_;
It will be possible to compile the code
req_.body() = body;
I tested it under CentOS7.

Related

Issue with writing InfluxDB code to ESP32 chip using OTA

I'm trying to write a program for my ESP32 that writes to InfluxDB but also maintains an OTA access server and it appears that the two functions are having some impact on each other that's causing the OTA server to not work (i.e. the OTA page does not appear when I enter the IP address into the browser). I've narrowed the problem down to the
client.writePoint(sensor)
function that InfluxDB uses to write data to buffer and I'm unsure of how to remedy that. The OTA functionality works when I comment out the line that references the above function. I've included this code below.
//PASTE THIS IN ABOVE EXISTING HEADERS
//#include <WiFi.h> //if file already has these libraries, remove it from one of the places
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
const char* host = "esp32";
const char* ssid = "ssid";
const char* password = "pwd";
WebServer server(80);
// end OTA header file
//BEGIN HEADER FILE
#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "TEST"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
/* Self inclusions -> Not from InfluxDB */
#define Vdd 3.3
#define Aout 35
#define LINEAR LOW
#define SQ_ROOT HIGH
const int R_0 = -1812; //Change this to your own R0 measurements
#include "max6675.h"
#include <WiFi.h>
#include <WiFiUdp.h>
/* End Self Inclusions */
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL "url"
// InfluxDB v2 server or cloud API authentication token ( Data -> Tokens -> MQ Sensors)
#define INFLUXDB_TOKEN "token"
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids )
#define INFLUXDB_ORG "org"
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Data -> Buckets)
#define INFLUXDB_BUCKET "bucket"
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// Examples:
// Pacific Time: "PST8PDT"
// Eastern: "EST5EDT"
// Japanesse: "JST-9"
// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3"
#define TZ_INFO "EST5EDT"
// InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Data Point
Point sensor("VOC_data"); // Data point
// END HEADER FILE
void setup() { //make sure this line appears one time only
Serial.begin(115200); //make sure there are not two serial/begin functions in setup
Serial.println("started"); //TS COMMENT
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("\n\nACCESS UPDATES AT: http://");
Serial.print(WiFi.localIP());
Serial.println("\n\n");
pinMode(Aout, INPUT);
// Add tags
sensor.addTag("device", DEVICE);
// Accurate time is necessary for certificate validation and writing in batches
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
/*use mdns for host name resolution*/
if (!MDNS.begin(host)) { //http://esp32.local
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
/*return index page which is stored in serverIndex */
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginIndex);
Serial.println("init1 complete"); //TS COMMENT
});
server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
Serial.println("init2 complete"); //TS COMMENT
});
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
Serial.println("init3 complete"); //TS COMMENT
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
Serial.println("init4 complete"); //TS COMMENT
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
Serial.println("Check at line 201");
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Serial.println("Check at line 207");
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Serial.println("Check at line 214");
Update.printError(Serial);
}
}
});
server.begin();
} //delete if void setup() line is deleted
void loop() { //make sure this line does not appear twice
server.handleClient();
float a0 = analogRead(Aout); // get raw reading from sensor
float v_o = a0 * 4.6 / 1023; // convert reading to volts
float R_S = (4.6-v_o) * 1000 / v_o; // apply formula for getting RS
float R_a = R_S/R_0; // formula for the ratio
float PPM = pow(R_a,-2.95) * 1000; //apply formula for getting PPM
float PPM_ALCOHOL = pow(-13.17*log(R_S/R_0) + 10.35 ,1);
//double PPM = pow(static_cast<double>(R_S/R_0),-2.95) * 1000;
//float PPMnew = a0*0.065156122+0.746160521;
sensor.clearFields();
// Store measured value into point
sensor.addField("VOC_Sensor", a0);
sensor.addField("VOC_PPM", PPM);
//sensor.addField("VOC_RS", R_S);
//sensor.addField("VOC_ALCOHOL", PPM_ALCOHOL);
/****************************** Self inclusions -> Not from InfluxDB ******************************/
Serial.print("Sensor Voltage: ");
Serial.print(v_o); //VOC concentration
Serial.println(" V"); //units
Serial.print("VOC Concentration calculation in arduino: ");
Serial.print(PPM); //VOC concentration
Serial.println(" PPM"); //units
Serial.print("Raw signal: ");
Serial.print(a0); //VOC concentration
Serial.println(" "); //units
delay(1000);
/***************************************************************************************************/
// Print what are we exactly writing
Serial.println(WiFi.localIP());
Serial.println("Line 286");
Serial.println(sensor.toLineProtocol());
// Write point
if (client.writePoint(sensor)) {
Serial.println("InfluxDB write successful");
} else {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Wait 200ms");
delay(200);
} //delete if void loop() line is deleted
The serial output displays
Connected to ssid
ACCESS UPDATES AT: ESP32_IP_ADDRESS
and then continues to display the "InfluxDB write successful" message with each data point.

How can I detect problems with user/password in the connection to MQTT broker at connection time using the libmosquitto library?

I'm testing MQTT mosquitto library with this small program:
/*
compile using:
$ gcc -o libmosq libmosq.c -lmosquitto
*/
#include <stdio.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct mosquitto *mosq = NULL;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL);
if(!mosq) {
fprintf(stderr, "Error: Out of memory.\n");
exit(1);
}
mosquitto_username_pw_set(mosq, "user1", "passwd1");
int resultCode = mosquitto_connect(mosq, "localhost", 1883, 60);
if (resultCode != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "connection error\n");
exit(1);
}
else {
printf("connection success\n");
}
// wait until control+C is done
sleep(1000000);
}
I'm running a MQTT broker (mosquitto broker version 1.6.10) in localhost at port 1883.
When I run the program I get "connection success" and I see in the mosquitto log:
iot-mosquitto | 2021-10-06T10:16:11: New connection from 172.17.0.1 on port 1883.
iot-mosquitto | 2021-10-06T10:16:11: New client connected from 172.17.0.1 as auto-51085B64-A53B-DBE1-DBFB-A6D9D702B69C (p2, c1, k60, u'user1').
I understand that in this case the connection is correct. So far so good.
However, if I use a wrong user/pass (e.g. mosquitto_username_pw_set(mosq, "user1", "xxxxx")) or if don't use user/pass (i.e. removing the mosquitto_username_pw_set() map) I get in the mosquitto broker log:
iot-mosquitto | 2021-10-06T10:27:58: New connection from 172.17.0.1 on port 1883.
iot-mosquitto | 2021-10-06T10:27:58: Socket error on client <unknown>, disconnecting.
which is fine. The problem is that in my program I get "connection success" instead of "connection error". In other words, I'm getting MOSQ_ERR_SUCCESS as return value for mosquitto_connect() instead of MOSQ_ERR_ERRNO.
Looking to the MQTT broker traces, it is like my program gets connected (which would explain the MOSQ_ERR_SUCCESS) but instanstanely it's disconnected...
How can I detect problems with user/password in the connection at connection time using the libmosquitto library, please?
Thanks in advance!
EDIT: I understand that there is some way to solve this, as mosquitto_sub (which I understand is based in the same library) is able to detect. Eg:
$ mosquitto_sub -p 1883 -t '#' -u user1 -P xxxxxx
Connection error: Connection Refused: not authorised.
I finally solved it using the following program:
/*
compile using:
$ gcc -o libmosq libmosq.c -lmosquitto
*/
#include <stdio.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <unistd.h>
void connection_callback(struct mosquitto* mosq, void *obj, int rc)
{
if (rc) {
printf("connection error: %d (%s)\n", rc, mosquitto_connack_string(rc));
exit(1);
}
else {
printf("connection success\n");
}
}
int main(int argc, char *argv[])
{
struct mosquitto *mosq = NULL;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL);
if(!mosq) {
fprintf(stderr, "Error: Out of memory.\n");
exit(1);
}
mosquitto_connect_callback_set(mosq, connection_callback);
mosquitto_username_pw_set(mosq, "user1", "passwd1");
int resultCode = mosquitto_connect(mosq, "localhost", 1883, 60);
if (resultCode != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "error calling mosquitto_connect\n");
exit(1);
}
int loop = mosquitto_loop_start(mosq);
if(loop != MOSQ_ERR_SUCCESS){
fprintf(stderr, "Unable to start loop: %i\n", loop);
exit(1);
}
// hang until control+C is done
sleep(1000000);
}
Main differences compared with the first version:
Using mosquitto_connect_callback_set() to set a callback function for the connection event
Using mosquitto_loop_start(). If I don't add this statement, the callback is not called upon connection
With this program I get the following message when connection is successful:
connection success
and this one when password is wrong or if I remove the mosquitto_username_pw_set() statement:
connection error: 5 (Connection Refused: not authorised.)

I can not subscribe with ESP8266-ESP32 in IBM Watson IOT

I want to subscribe to the "iot-2/evt/status/fmt/json" topic with ESP8266 on IBM Watson IOT. The connection is established but it is disconnecting again. So, reconnecting the MQTT client to … and subscribe to iot-2 / cmd / + / fmt / + OK. This cycle continues. Why is the connection broken?
My ESP8266 code is as follows.
I used an ESP8266-12E NodeMCU, I’ve created an Android app for the publisher.
/*
Basic ESP8266 MQTT example
This sketch demonstrates the capabilities of the pubsub library in combination
with the ESP8266 board/library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
else switch it off
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
To install the ESP8266 board, (using Arduino 1.6.4+):
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
- Select your ESP8266 in "Tools -> Board"
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
//#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
//ZYxel
#define ssid "......."
#define password ".................."
//GES ARGE
#define ssid2 "..............." // WiFi SSID
#define password2 "............." // WiFi password
#define spi_ss_pin SS
#define ORG "............"
#define DEVICE_TYPE "........."
#define DEVICE_ID "..........."
#define TOKEN "................"
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
int mqttPort=1883;
const char topic[] = "iot-2/cmd/status/fmt/json"; //"iot-2/cmd/status/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
WiFiClient wifiClient;
void callback(char* topic, byte* payload, unsigned int payloadLength) ;
PubSubClient client(server, 1883, callback, wifiClient);
void setup() {
Serial.begin(115200);
Serial.println();
wifiConnect();
mqttConnect();
}
void loop() {
if (!client.loop()) {
mqttConnect();
}
}
void wifiConnect() {
Serial.print("Connecting to "); Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());
}
void mqttConnect() {
if (!client.connected()) {
Serial.print("Reconnecting MQTT client to "); Serial.println(server);
while (!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
initManagedDevice();
Serial.println();
}
}
void initManagedDevice() {
if (client.subscribe(topic)) {
Serial.println("subscribe to cmd OK");
} else {
Serial.println("subscribe to cmd FAILED");
}
}
void callback(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for topic: "); Serial.println(topic);
for (int i = 0; i < payloadLength; i++) {
Serial.print((char)payload[i]);
}
}
A device ("use-token-auth" authentication type) cannot subscribe to a topic like "iot-2/evt/status/fmt/json" only "iot-2/cmd/status/fmt/json" is allowed.
What you need to do is to generate an API key and token and authenticate as an application:
The following example shows a typical API key:
a-orgId-a84ps90Ajs
The following example shows a typical authentication token:
MP$08VKz!8rXwnR-Q*
When you make an MQTT connection by using an API key, ensure that the following guidelines are applied:
The MQTT client ID is in the format: a:orgId:appId
The MQTT user name is the API key (for example, a-orgId-a84ps90Ajs)
The MQTT password is the authentication token (for example, MP$08VKz!8rXwnR-Q*)
After that you can subscribe to topic like iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string. So it should be:
iot-2/type/yourDeviceType/id/yourDeviceId/evt/status/fmt/json
You can use the same for commands: this how the topic should look like
iot-2/type/device_type/id/device_id/cmd/command_id/fmt/format_string

The base64 encode formatted output from Arduino HMAC-SHA1 does not match with JAVA/python/online tool

I am working on an Arduino project which is required an authorized authentication based on OAuth 1.0 to connects to the cloud. This is alike [Authorizing a request to Twitter API][1], and I am stuck in the step of [Creating a signature][2]. The whole process of creating a signature requires algorithms like encodeURL, base64encode, and hmac-sha1. On my Arduino project, I use Cryptosuite(link 3) library for hmac-sha1 and arduino-base64(link 4) library for base64encode. Both of them are working fine separately. However, I need to get a base64-formatted output of hmac-sha1. So I have tried this:
#include <avr/pgmspace.h>
#include <sha1.h>
#include <Base64.h>
uint8_t *in, out, i;
char b64[29];
static const char PROGMEM b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char key[] = "testKey";
char basestring[] = "testing";
void printHash(uint8_t* hash) {
int i;
for (i=0; i<20; i++) {
Serial.print("0123456789abcdef"[hash[i]>>4]);
Serial.print("0123456789abcdef"[hash[i]&0xf]);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
Serial.print("Result:");
Sha1.initHmac((uint8_t*)key, strlen(key));
Sha1.print(basestring);
printHash(Sha1.resultHmac());
Serial.println();
// encoding
char* input;
input = (char*)(Sha1.resultHmac());
int inputLen = strlen(input);
int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];
// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, input, inputLen);
Serial.print("base64 result: ");
Serial.println(encoded);
}
void loop() {
}
The output of printHash that I got is 60d41271d43b875b791e2d54c34bf3f018a29763, which is exactly same with the online verification tool(link 5).
However, I supposed to get YNQScdQ7h1t5Hi1Uw0vz8Biil2M= for the base64 result. But I got L18B0HicKRhuxmB6SIFpZP+DpHxU which seems wrong. I have also tried to write a JAVA program and a python program, which also said that the output of the base64 result should be YNQScdQ7h1t5Hi1Uw0vz8Biil2M=
I also found this post: Issues talking between Arduino SHA1-HMAC and base64 encoding and Python(link 6). I have also tried the tidy function it mentioned from Adafruit-Tweet-Receipt(link 7).
// base64-encode SHA-1 hash output. This is NOT a general-purpose base64
// encoder! It's stripped down for the fixed-length hash -- always 20
// bytes input, always 27 chars output + '='.
for(in = Sha1.resultHmac(), out=0; ; in += 3) { // octets to sextets
b64[out++] = in[0] >> 2;
b64[out++] = ((in[0] & 0x03) << 4) | (in[1] >> 4);
if(out >= 26) break;
b64[out++] = ((in[1] & 0x0f) << 2) | (in[2] >> 6);
b64[out++] = in[2] & 0x3f;
}
b64[out] = (in[1] & 0x0f) << 2;
// Remap sextets to base64 ASCII chars
for(i=0; i<=out; i++) b64[i] = pgm_read_byte(&b64chars[b64[i]]);
b64[i++] = '=';
b64[i++] = 0;
Is there any mistake I've made in here?
Thanks!
So full example will be:
#include <avr/pgmspace.h>
#include <sha1.h>
#include <Base64.h>
char key[] = "testKey";
char basestring[] = "testing";
void printHash(uint8_t* hash) {
for (int i=0; i<20; i++) {
Serial.print("0123456789abcdef"[hash[i]>>4]);
Serial.print("0123456789abcdef"[hash[i]&0xf]);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
Serial.print("Input: ");
Serial.println(basestring);
Serial.print("Key: ");
Serial.println(key);
Serial.print("Hmac-sha1 (hex): ");
Sha1.initHmac((uint8_t*)key, strlen(key));
Sha1.print(basestring);
uint8_t *hash;
hash = Sha1.resultHmac();
printHash(hash);
// base64 encoding
char* input = (char*) hash;
int inputLen = strlen(input) - 1; // skip null termination
int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];
// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, input, inputLen);
Serial.print("Hmac-sha1 (base64): ");
Serial.println(encoded);
}
void loop() { }
which outputs:
Input: testing
Key: testKey
Hmac-sha1 (hex): 60d41271d43b875b791e2d54c34bf3f018a29763
Hmac-sha1 (base64): YNQScdQ7h1t5Hi1Uw0vz8Biil2M=

How to get task port of SpringBoard in iOS7 (Jailbroken)?

I know we can use contextIdAtPosition and taskPortOfContextId to get the mach_port_t of the front top app, but when inside some app, we can not use contextIdAtPosition to get the context id of SpringBoard (it's at background), so how can we get the mach_port_t of SpringBoard? Thank you!
according to http://theiphonewiki.com/wiki//System/Library/LaunchDaemons/com.apple.SpringBoard.plist, the SpringBoard has exposed a lot of services. two of them might (or might not) be of your interests:
"com.apple.iohideventsystem"
"com.apple.springboard"
Here is the sample code to query the ports by service names.
#include <mach/mach.h>
#include "bootstrap.h"
#include <stdio.h>
#include <stdlib.h>
#define CHECK_MACH_ERROR(a) do {kern_return_t rr = (a); if ((rr) != KERN_SUCCESS) \
{ printf("Mach error %x (%s) on line %d of file %s\n", (rr), mach_error_string((rr)), __LINE__, __FILE__); abort(); } } while (0)
int main(int argc, char **argv, char **envp)
{
mach_port_t bp = MACH_PORT_NULL;
mach_port_t sp = MACH_PORT_NULL;
kern_return_t err = task_get_bootstrap_port(mach_task_self(), &bp);
CHECK_MACH_ERROR(err);
printf("bp:%d\n", bp);
err = bootstrap_look_up(bp, "com.apple.iohideventsystem", &sp);
CHECK_MACH_ERROR(err);
printf("iohideventsystem:%d\n", sp);
err = bootstrap_look_up(bp, "com.apple.springboard", &sp);
CHECK_MACH_ERROR(err);
printf("springboard:%d\n", sp);
// need to deallocate ports before exit
return 0;
}
The output:
my-iPad:~ root# /usr/bin/port_query
bp:519
iohideventsystem:4099
springboard:4355
There is a SpringboardService framework.
It has a function SBSSpringBoardServerPort() which returns Springboard mach port.
Note: Each application may have multiple mach ports, so I am not sure that it's one which you need.

Resources