Esempio n. 1
0
void initList(int strc, char **strv) {
	if ( strc <= 0) return;

	stringbuffer_t *sb = stringbuffer_init();

	int i;
	for (i = 0; i < strc; ++i) {
		stringbuffer_append(sb, strv[i]);
		stringbuffer_append(sb, " ");
	}

	gList = stringbuffer_text(sb);

	stringbuffer_free(sb);
}
Esempio n. 2
0
void doUpdate() {
	stringbuffer_t *sb = stringbuffer_init();
	stringbuffer_append(sb, gPath);
	stringbuffer_append(sb, "/");
	switch (gSystem) {
		case SYSTEM_YUM:
			stringbuffer_append(sb, "yum update");
			break;
		case SYSTEM_APT:
			stringbuffer_append(sb, "apt-get update");
			break;
		default:
			fprintf(stderr, "ERROR: Undefined system.\n");
			exit(EXIT_FAILURE);
	}
	stringbuffer_append(sb, " ");
	stringbuffer_append(sb, gList);

	system(stringbuffer_getTextPointer(sb));

	stringbuffer_free(sb);
}
void update_time(void) {
  LOG_FUNC();

  if (!ui_updates_enabled) {
      LOG(LOG_FACEUPDATE, "static void update_time(): not done, not enabled");
      return;
  }

  time_t temp = time(NULL);
  struct tm *tick_time = localtime(&temp);

  LOG(LOG_FACEUPDATE, "static void update_time(): display plain time");
  // Use a static (long lived) buffer for the numeric time.
  static char time[] = "00:00";
  // Write the current hours and minutes into the buffer, considerung the
  // 12/24h style.
  if(clock_is_24h_style() == true) {
    strftime(time, sizeof("00:00"), "%H:%M", tick_time);
  } else {
    strftime(time, sizeof("00:00"), "%I:%M", tick_time);
  }
  // Display time in respective layer.
  text_layer_set_text(s_timelayer, time);

  int weekday = tick_time->tm_wday-1;
  if (weekday < 0) weekday += 7;
  LOG(LOG_FACEUPDATE, "static void update_time(): display date");
  LOG_EXT(LOG_FACEUPDATE, "static void update_time(): day: %d", weekday);
  LOG_EXT(LOG_FACEUPDATE, "static void update_time(): mday: %d", tick_time->tm_mday);
  LOG_EXT(LOG_FACEUPDATE, "static void update_time(): month: %d", tick_time->tm_mon);
  // Use a static (long lived) buffer for the numeric date.
  static char date[80];
  strftime(date, 80, "%a, %d. %b", tick_time);
  // Display date in respective layer.
  LOG(LOG_FACEUPDATE, "updating s_date_layer");
  text_layer_set_text(s_datelayer, date);

  // Fetch and print BlueTooth status information.
  LOG(LOG_FACEUPDATE, "updating s_info1_layer");
  text_layer_set_text(s_btlayer, bt_state_string);
  if (bt_state) {
    bitmap_layer_set_bitmap(s_bitmaplayer_bt, s_res_image_bt_active);
  } else {
    bitmap_layer_set_bitmap(s_bitmaplayer_bt, s_res_image_bt_passive);
  }

  LOG(LOG_FACEUPDATE, "updating s_batterylayer");
  static char battery_state_str[6];
  if (battery_state.is_plugged) {
    if (battery_state.is_charging) {
      snprintf(battery_state_str, 6, "charging");
    } else {
      snprintf(battery_state_str, 6, "full");
    }
  } else {
    snprintf(battery_state_str, 6, "%d%%", battery_state.charge_percent);
  }
  text_layer_set_text(s_batterylayer, battery_state_str);
   
  if (storage.last_full_timestamp != -1) {
    static stringbuffer outbound;
    stringbuffer_init(&outbound);
    stringbuffer_append_ti(&outbound, temp - storage.last_full_timestamp);
    text_layer_set_text(s_outboundlayer, outbound.retval);
  } else if (battery_state.is_plugged) {
    text_layer_set_text(s_outboundlayer, "plugged");
  } else {
    text_layer_set_text(s_outboundlayer, "-");
  }
  static stringbuffer inbound;
  stringbuffer_init(&inbound);
  stringbuffer_append_ti(&inbound, battery_estimate_secs);
  text_layer_set_text(s_inboundlayer, inbound.retval);
}
Esempio n. 4
0
int main(int argc, char **argv) {
	CURL *curl;
	CURLcode res;
	struct curl_slist* slist = NULL;
	struct stringbuffer buffer;
	long resp_code = 0;
	char *token = NULL;

	curl = curl_easy_init();
	stringbuffer_init(&buffer);

	if (curl) {
		/** Let it be known that I refuse to write manual, string-quoted JSON. */
		slist = curl_slist_append(slist, "Content-type: application/json");

		json_object *body_obj = json_object_new_object();
		json_object *auth_obj = json_object_new_object();
		json_object *cred_obj = json_object_new_object();
		json_object *tenant_str = json_object_new_string(KEYSTONE_TENANT);
		json_object *user_str = json_object_new_string(KEYSTONE_USER);
		json_object *pass_str = json_object_new_string(KEYSTONE_PASS);

		json_object_object_add(body_obj, "auth", auth_obj);
		json_object_object_add(auth_obj, "passwordCredentials", cred_obj);
		json_object_object_add(auth_obj, "tenantName", tenant_str);
		json_object_object_add(cred_obj, "username", user_str);
		json_object_object_add(cred_obj, "password", pass_str);

		const char* body = json_object_to_json_string(body_obj);
		//printf("body[%d]:\n%s\n\n", strlen(body), body);

		curl_easy_setopt(curl, CURLOPT_URL, KEYSTONE_URL);
		curl_easy_setopt(curl, CURLOPT_POST, 1);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (void*) http_received_data);

		res = curl_easy_perform(curl);

		json_object_put(body_obj);
		json_object_put(auth_obj);
		json_object_put(cred_obj);
		json_object_put(user_str);
		json_object_put(pass_str);
		json_object_put(tenant_str);

		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &resp_code);

		//printf("HTTP %d\n", resp_code);
		//printf("body[%d]\n%s\n\n", buffer.len, buffer.ptr);

		if (res != CURLE_OK) {
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
					curl_easy_strerror(res));
		}
		else if (resp_code == 200) {
			json_object *json = json_tokener_parse(buffer.ptr);
			json_object *access_obj = json_object_object_get(json, "access");
			json_object *token_obj = json_object_object_get(access_obj, "token");
			json_object *id_obj = json_object_object_get(token_obj, "id");

			const char* tmp_token = json_object_get_string(id_obj);
			token = malloc(strlen(tmp_token) + 1);
			strcpy(token, tmp_token);

			json_object_put(id_obj);
			json_object_put(token_obj);
			json_object_put(access_obj);
			json_object_put(json);
		}
		else {
			fprintf(stderr, "Could not authenticate (HTTP %d received).\n", resp_code);
		}

		stringbuffer_free(&buffer);
		curl_easy_cleanup(curl);
		curl_slist_free_all(slist);
	}

	if (token != NULL) {
		printf("token is %s\n", token);
	}

	return 0;
}